Skip to content

Commit

Permalink
[WIP] config refactor
Browse files Browse the repository at this point in the history
Signed-off-by: He Xian <[email protected]>
  • Loading branch information
hexian000 committed Oct 7, 2024
1 parent d836d32 commit dd53d4a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
22 changes: 12 additions & 10 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"tunnel": [
{
"muxlisten": "0.0.0.0:12345",
"dial": "127.0.0.1:8080"
},
{
"listen": "127.0.0.1:8080",
"muxdial": "127.0.0.1:12345"
}
],
"peername": "client1",
"muxlisten": "127.1.0.1:12345",
"services": {
"socks": "127.0.1.1:1081"
},
"peers": {
"client2": {
"addr": "127.1.0.2:12345",
"listen": "127.1.0.2:1081",
"peerservice": "socks"
}
},
"cert": "server-cert.pem",
"key": "server-key.pem",
"authcerts": [
Expand Down
4 changes: 2 additions & 2 deletions v3/cmd/tlswrapper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func parseFlags() string {

func main() {
path := parseFlags()
cfg, err := config.ReadFile(path)
cfg, err := config.LoadFile(path)
if err != nil {
slog.Fatal("read config: ", err)
os.Exit(1)
Expand Down Expand Up @@ -59,7 +59,7 @@ func main() {
}
// reload
_, _ = sd.Notify(sd.Reloading)
cfg, err := config.ReadFile(path)
cfg, err := config.LoadFile(path)
if err != nil {
slog.Error("read config: ", err)
continue
Expand Down
10 changes: 5 additions & 5 deletions v3/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ type File struct {
LogLevel int `json:"loglevel"`
}

var DefaultConfig = File{
var Default = File{
ServerName: "example.com",
NoDelay: true,
StartupLimitStart: 10,
Expand All @@ -101,15 +101,15 @@ var DefaultConfig = File{
LogLevel: slog.LevelNotice,
}

var DefaultTunnelConfig = Tunnel{
var TunnelDefault = Tunnel{
Redial: true,
KeepAlive: 25, // every 25s
AcceptBacklog: 256,
StreamWindow: 256 * 1024, // 256 KiB
}

func Load(b []byte) (*File, error) {
cfg := DefaultConfig
cfg := Default
if err := json.Unmarshal(b, &cfg); err != nil {
return nil, err
}
Expand All @@ -123,7 +123,7 @@ func Load(b []byte) (*File, error) {
return &cfg, nil
}

func ReadFile(path string) (*File, error) {
func LoadFile(path string) (*File, error) {
b, err := os.ReadFile(path)
if err != nil {
return nil, err
Expand Down Expand Up @@ -238,7 +238,7 @@ func (c *File) FindService(service string) string {

// NewMuxConfig creates yamux.Config
func (c *File) NewMuxConfig() *yamux.Config {
t := DefaultTunnelConfig
t := TunnelDefault
keepAliveInterval := time.Duration(c.ServerKeepAlive) * time.Second
enableKeepAlive := keepAliveInterval >= time.Second
if !enableKeepAlive {
Expand Down
6 changes: 3 additions & 3 deletions v3/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,10 @@ func (s *Server) Serve(listener net.Listener, handler Handler) {
func (s *Server) Listen(addr string) (net.Listener, error) {
listener, err := net.Listen(network, addr)
if err != nil {
slog.Error("listen ", addr, ": ", formats.Error(err))
slog.Errorf("listen %s: %s", addr, formats.Error(err))
return listener, err
}
slog.Info("listen: ", listener.Addr())
slog.Infof("listen: %v", listener.Addr())
s.listeners[addr] = listener
return listener, err
}
Expand Down Expand Up @@ -234,7 +234,7 @@ func (s *Server) Start() error {
// Shutdown gracefully
func (s *Server) Shutdown() error {
for addr, listener := range s.listeners {
slog.Info("listener close: ", listener.Addr())
slog.Infof("listener close: %v", listener.Addr())
ioClose(listener)
delete(s.listeners, addr)
}
Expand Down
2 changes: 1 addition & 1 deletion v3/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (t *tunnel) Start() error {
if err != nil {
return err
}
slog.Noticef("forward listen: %v", l.Addr())
slog.Noticef("tunnel %q: listen %v", t.peerName, l.Addr())
h := &TunnelHandler{l: l, s: t.s, t: t}
if err := t.s.g.Go(func() {
t.s.Serve(l, h)
Expand Down

0 comments on commit dd53d4a

Please sign in to comment.