Skip to content

Commit bb5f3b0

Browse files
committed
fix: Config generation
1 parent 943c540 commit bb5f3b0

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

common/jsonx/raw_json.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ var (
1010
)
1111

1212
func (r RawJSON) MarshalJSON() ([]byte, error) {
13+
if r == nil {
14+
return []byte("null"), nil
15+
}
1316
return r, nil
1417
}
1518

config/root.go

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package config
33
import (
44
"context"
55
"encoding/json"
6+
"errors"
7+
"github.com/layou233/zbproxy/v3/common"
8+
"github.com/layou233/zbproxy/v3/common/jsonx"
69
"os"
710
"time"
811

@@ -129,7 +132,60 @@ func LoadConfigFromFile(ctx context.Context, filePath string, watch bool, logger
129132
var rawConfig _Root
130133
err := loadContent(&rawConfig, filePath)
131134
if err != nil {
132-
return nil, err
135+
if errors.Is(err, os.ErrNotExist) {
136+
logger.Warn().Msg("Config file does not exist, generating a new one...")
137+
rawConfig = _Root{
138+
Log: Log{
139+
Level: log.DebugLevel,
140+
},
141+
Services: []*Service{
142+
{
143+
Name: "Hypixel-in",
144+
Listen: 25565,
145+
},
146+
},
147+
Router: Router{
148+
Rules: []*Rule{
149+
{
150+
Type: "always",
151+
Sniff: jsonx.Listable[string]{"minecraft"},
152+
},
153+
},
154+
DefaultOutbound: "Hypixel-out",
155+
},
156+
Outbounds: []*Outbound{
157+
{
158+
Name: "Hypixel-out",
159+
TargetAddress: "mc.hypixel.net",
160+
TargetPort: 25565,
161+
Minecraft: &MinecraftService{
162+
EnableHostnameRewrite: true,
163+
OnlineCount: onlineCount{
164+
Max: 20,
165+
Online: -1,
166+
},
167+
MotdFavicon: "{DEFAULT_MOTD}",
168+
MotdDescription: "§d{NAME}§e, provided by §a§o{INFO}§r\n§c§lProxy for §6§n{HOST}:{PORT}§r",
169+
},
170+
},
171+
},
172+
Lists: map[string]set.StringSet{},
173+
}
174+
var file *os.File
175+
file, err = os.Create("ZBProxy.json")
176+
if err != nil {
177+
return nil, common.Cause("create config file: ", err)
178+
}
179+
encoder := json.NewEncoder(file)
180+
encoder.SetEscapeHTML(false)
181+
encoder.SetIndent("", " ")
182+
err = encoder.Encode(&rawConfig)
183+
if err != nil {
184+
return nil, common.Cause("generate config: ", err)
185+
}
186+
} else {
187+
return nil, common.Cause("load config: ", err)
188+
}
133189
}
134190
root := &Root{
135191
Log: rawConfig.Log,

config/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type Router struct {
99

1010
type Rule struct {
1111
Type string
12-
Parameter jsonx.RawJSON
12+
Parameter jsonx.RawJSON `json:",omitempty"`
1313
//SubRules []Rule `json:",omitempty"`
1414
Rewrite RuleRewrite `json:",omitempty"`
1515
Sniff jsonx.Listable[string] `json:",omitempty"`

config/service.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import "github.com/layou233/zbproxy/v3/common/network"
44

55
type Service struct {
66
Name string
7-
TargetAddress string
8-
TargetPort uint16
7+
TargetAddress string `json:",omitempty"`
8+
TargetPort uint16 `json:",omitempty"`
99
Listen uint16
10-
Flow string
1110

1211
IPAccess access `json:",omitempty"`
1312
Minecraft *MinecraftService `json:",omitempty"`

0 commit comments

Comments
 (0)