Skip to content

Commit

Permalink
refactor: change config path and add extra check for tun mode
Browse files Browse the repository at this point in the history
  • Loading branch information
cxz66666 committed Feb 6, 2024
1 parent 761c21d commit 7105140
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 105 deletions.
86 changes: 86 additions & 0 deletions configs/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package configs

type (
Config struct {
ServerAddress string
ServerPort int
Username string
Password string
DisableServerConfig bool
DisableZJUConfig bool
DisableZJUDNS bool
DisableMultiLine bool
ProxyAll bool
SocksBind string
SocksUser string
SocksPasswd string
HTTPBind string
ShadowsocksURL string
TUNMode bool
AddRoute bool
DNSTTL uint64
DisableKeepAlive bool
ZJUDNSServer string
SecondaryDNSServer string
DNSServerBind string
DNSHijack bool
DebugDump bool
PortForwardingList []SinglePortForwarding
CustomDNSList []SingleCustomDNS
CustomProxyDomain []string
TwfID string
}

SinglePortForwarding struct {
NetworkType string
BindAddress string
RemoteAddress string
}

SingleCustomDNS struct {
HostName string `toml:"host_name"`
IP string `toml:"ip"`
}
)

type (
ConfigTOML struct {
ServerAddress *string `toml:"server_address"`
ServerPort *int `toml:"server_port"`
Username *string `toml:"username"`
Password *string `toml:"password"`
DisableServerConfig *bool `toml:"disable_server_config"`
DisableZJUConfig *bool `toml:"disable_zju_config"`
DisableZJUDNS *bool `toml:"disable_zju_dns"`
DisableMultiLine *bool `toml:"disable_multi_line"`
ProxyAll *bool `toml:"proxy_all"`
SocksBind *string `toml:"socks_bind"`
SocksUser *string `toml:"socks_user"`
SocksPasswd *string `toml:"socks_passwd"`
HTTPBind *string `toml:"http_bind"`
ShadowsocksURL *string `toml:"shadowsocks_url"`
TUNMode *bool `toml:"tun_mode"`
AddRoute *bool `toml:"add_route"`
DNSTTL *uint64 `toml:"dns_ttl"`
DisableKeepAlive *bool `toml:"disable_keep_alive"`
ZJUDNSServer *string `toml:"zju_dns_server"`
SecondaryDNSServer *string `toml:"secondary_dns_server"`
DNSServerBind *string `toml:"dns_server_bind"`
DNSHijack *bool `toml:"dns_hijack"`
DebugDump *bool `toml:"debug_dump"`
PortForwarding []SinglePortForwardingTOML `toml:"port_forwarding"`
CustomDNS []SingleCustomDNSTOML `toml:"custom_dns"`
CustomProxyDomain []string `toml:"custom_proxy_domain"`
}

SinglePortForwardingTOML struct {
NetworkType *string `toml:"network_type"`
BindAddress *string `toml:"bind_address"`
RemoteAddress *string `toml:"remote_address"`
}

SingleCustomDNSTOML struct {
HostName *string `toml:"host_name"`
IP *string `toml:"ip"`
}
)
100 changes: 8 additions & 92 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,96 +5,12 @@ import (
"flag"
"fmt"
"github.com/BurntSushi/toml"
"github.com/mythologyli/zju-connect/configs"
"os"
"regexp"
"strings"
)

type (
Config struct {
ServerAddress string
ServerPort int
Username string
Password string
DisableServerConfig bool
DisableZJUConfig bool
DisableZJUDNS bool
DisableMultiLine bool
ProxyAll bool
SocksBind string
SocksUser string
SocksPasswd string
HTTPBind string
ShadowsocksURL string
TUNMode bool
AddRoute bool
DNSTTL uint64
DisableKeepAlive bool
ZJUDNSServer string
SecondaryDNSServer string
DNSServerBind string
DNSHijack bool
DebugDump bool
PortForwardingList []SinglePortForwarding
CustomDNSList []SingleCustomDNS
CustomProxyDomain []string
TwfID string
}

SinglePortForwarding struct {
NetworkType string
BindAddress string
RemoteAddress string
}

SingleCustomDNS struct {
HostName string `toml:"host_name"`
IP string `toml:"ip"`
}
)

type (
ConfigTOML struct {
ServerAddress *string `toml:"server_address"`
ServerPort *int `toml:"server_port"`
Username *string `toml:"username"`
Password *string `toml:"password"`
DisableServerConfig *bool `toml:"disable_server_config"`
DisableZJUConfig *bool `toml:"disable_zju_config"`
DisableZJUDNS *bool `toml:"disable_zju_dns"`
DisableMultiLine *bool `toml:"disable_multi_line"`
ProxyAll *bool `toml:"proxy_all"`
SocksBind *string `toml:"socks_bind"`
SocksUser *string `toml:"socks_user"`
SocksPasswd *string `toml:"socks_passwd"`
HTTPBind *string `toml:"http_bind"`
ShadowsocksURL *string `toml:"shadowsocks_url"`
TUNMode *bool `toml:"tun_mode"`
AddRoute *bool `toml:"add_route"`
DNSTTL *uint64 `toml:"dns_ttl"`
DisableKeepAlive *bool `toml:"disable_keep_alive"`
ZJUDNSServer *string `toml:"zju_dns_server"`
SecondaryDNSServer *string `toml:"secondary_dns_server"`
DNSServerBind *string `toml:"dns_server_bind"`
DNSHijack *bool `toml:"dns_hijack"`
DebugDump *bool `toml:"debug_dump"`
PortForwarding []SinglePortForwardingTOML `toml:"port_forwarding"`
CustomDNS []SingleCustomDNSTOML `toml:"custom_dns"`
CustomProxyDomain []string `toml:"custom_proxy_domain"`
}

SinglePortForwardingTOML struct {
NetworkType *string `toml:"network_type"`
BindAddress *string `toml:"bind_address"`
RemoteAddress *string `toml:"remote_address"`
}

SingleCustomDNSTOML struct {
HostName *string `toml:"host_name"`
IP *string `toml:"ip"`
}
)

func getTOMLVal[T int | uint64 | string | bool](valPointer *T, defaultVal T) T {
if valPointer == nil {
return defaultVal
Expand All @@ -103,8 +19,8 @@ func getTOMLVal[T int | uint64 | string | bool](valPointer *T, defaultVal T) T {
}
}

func parseTOMLConfig(configFile string, conf *Config) error {
var confTOML ConfigTOML
func parseTOMLConfig(configFile string, conf *configs.Config) error {
var confTOML configs.ConfigTOML

_, err := toml.DecodeFile(configFile, &confTOML)
if err != nil {
Expand Down Expand Up @@ -148,7 +64,7 @@ func parseTOMLConfig(configFile string, conf *Config) error {
return errors.New("ZJU Connect: remote address is not set")
}

conf.PortForwardingList = append(conf.PortForwardingList, SinglePortForwarding{
conf.PortForwardingList = append(conf.PortForwardingList, configs.SinglePortForwarding{
NetworkType: *singlePortForwarding.NetworkType,
BindAddress: *singlePortForwarding.BindAddress,
RemoteAddress: *singlePortForwarding.RemoteAddress,
Expand All @@ -165,7 +81,7 @@ func parseTOMLConfig(configFile string, conf *Config) error {
return errors.New("ZJU Connect: IP is not set")
}

conf.CustomDNSList = append(conf.CustomDNSList, SingleCustomDNS{
conf.CustomDNSList = append(conf.CustomDNSList, configs.SingleCustomDNS{
HostName: *singleCustomDns.HostName,
IP: *singleCustomDns.IP,
})
Expand Down Expand Up @@ -241,7 +157,7 @@ func init() {
os.Exit(1)
}

conf.PortForwardingList = append(conf.PortForwardingList, SinglePortForwarding{
conf.PortForwardingList = append(conf.PortForwardingList, configs.SinglePortForwarding{
NetworkType: "tcp",
BindAddress: addressStringList[0],
RemoteAddress: addressStringList[1],
Expand All @@ -258,7 +174,7 @@ func init() {
os.Exit(1)
}

conf.PortForwardingList = append(conf.PortForwardingList, SinglePortForwarding{
conf.PortForwardingList = append(conf.PortForwardingList, configs.SinglePortForwarding{
NetworkType: "udp",
BindAddress: addressStringList[0],
RemoteAddress: addressStringList[1],
Expand All @@ -275,7 +191,7 @@ func init() {
os.Exit(1)
}

conf.CustomDNSList = append(conf.CustomDNSList, SingleCustomDNS{
conf.CustomDNSList = append(conf.CustomDNSList, configs.SingleCustomDNS{
HostName: dnsStringSplit[0],
IP: dnsStringSplit[1],
})
Expand Down
7 changes: 4 additions & 3 deletions internal/hook_func/initial_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package hook_func

import (
"context"
"github.com/mythologyli/zju-connect/configs"
"github.com/mythologyli/zju-connect/log"
)

type InitialFunc func(ctx context.Context) error
type InitialFunc func(ctx context.Context, config configs.Config) error
type InitialItem struct {
f InitialFunc
name string
Expand All @@ -22,11 +23,11 @@ func RegisterInitialFunc(execName string, fun InitialFunc) {
})
}

func ExecInitialFunc(ctx context.Context) []error {
func ExecInitialFunc(ctx context.Context, config configs.Config) []error {
var errList []error
for _, item := range initialFuncList {
log.Println("Exec func on initial:", item.name)
if err := item.f(ctx); err != nil {
if err := item.f(ctx, config); err != nil {
errList = append(errList, err)
log.Println("Exec func on initial ", item.name, "failed:", err)
} else {
Expand Down
15 changes: 14 additions & 1 deletion internal/hook_func/initial_func_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@ package hook_func

import (
"context"
"errors"
"github.com/mythologyli/zju-connect/configs"
"os"
"os/user"
)

func init() {
RegisterInitialFunc("clean resolver file", func(ctx context.Context) error {
RegisterInitialFunc("clean resolver file", func(ctx context.Context, config configs.Config) error {
// discard error
_ = os.Remove("/etc/resolver/zju.edu.cn")
_ = os.Remove("/etc/resolver/cc98.org")
return nil
})
RegisterInitialFunc("check tun mode cap", func(ctx context.Context, config configs.Config) error {
// discard error
if config.TUNMode {
current, _ := user.Current()
if current.Uid != "0" {
return errors.New("请使用sudo运行TUN模式")
}
}
return nil
})
}
18 changes: 17 additions & 1 deletion internal/hook_func/initial_func_linux.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
package hook_func

func init() {
import (
"context"
"github.com/mythologyli/zju-connect/configs"
"github.com/mythologyli/zju-connect/log"
"os/user"
)

func init() {
RegisterInitialFunc("check tun mode cap", func(ctx context.Context, config configs.Config) error {
// discard error
if config.TUNMode {
current, _ := user.Current()
if current.Uid != "0" {
log.Println("检测到TUN模式,但是当前用户不是root,可能会导致无法使用,如果遇到问题请使用sudo运行")
}
}
return nil
})
}
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"fmt"
"github.com/mythologyli/zju-connect/client"
"github.com/mythologyli/zju-connect/configs"
"github.com/mythologyli/zju-connect/dial"
"github.com/mythologyli/zju-connect/internal/hook_func"
"github.com/mythologyli/zju-connect/log"
Expand All @@ -21,7 +22,7 @@ import (
"syscall"
)

var conf Config
var conf configs.Config

const zjuConnectVersion = "0.7.0"

Expand All @@ -33,9 +34,9 @@ func main() {
log.EnableDebug()
}

if errs := hook_func.ExecInitialFunc(context.Background()); errs != nil {
if errs := hook_func.ExecInitialFunc(context.Background(), conf); errs != nil {
for _, err := range errs {
log.Printf("Initial ZJU-Connect failed:", err)
log.Printf("Initial ZJU-Connect failed: %s", err)
}
os.Exit(1)
}
Expand Down Expand Up @@ -176,7 +177,7 @@ func main() {
log.Println("Shutdown ZJU-Connect ......")
if errs := hook_func.ExecTerminalFunc(context.Background()); errs != nil {
for _, err := range errs {
log.Printf("Shutdown ZJU-Connect failed:", err)
log.Printf("Shutdown ZJU-Connect failed: %s", err)
}
} else {
log.Println("Shutdown ZJU-Connect success, Bye~")
Expand Down
9 changes: 5 additions & 4 deletions main_tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"fmt"
"github.com/mythologyli/zju-connect/client"
"github.com/mythologyli/zju-connect/configs"
"github.com/mythologyli/zju-connect/dial"
"github.com/mythologyli/zju-connect/internal/hook_func"
"github.com/mythologyli/zju-connect/log"
Expand All @@ -19,7 +20,7 @@ import (
"syscall"
)

var conf Config
var conf configs.Config

const zjuConnectVersion = "0.7.0-tun-only"

Expand All @@ -31,9 +32,9 @@ func main() {
log.EnableDebug()
}

if errs := hook_func.ExecInitialFunc(context.Background()); errs != nil {
if errs := hook_func.ExecInitialFunc(context.Background(), conf); errs != nil {
for _, err := range errs {
log.Printf("Initial ZJU-Connect failed:", err)
log.Printf("Initial ZJU-Connect failed: %s", err)
}
os.Exit(1)
}
Expand Down Expand Up @@ -162,7 +163,7 @@ func main() {
log.Println("Shutdown ZJU-Connect ......")
if errs := hook_func.ExecTerminalFunc(context.Background()); errs != nil {
for _, err := range errs {
log.Printf("Shutdown ZJU-Connect failed:", err)
log.Printf("Shutdown ZJU-Connect failed: %s", err)
}
} else {
log.Println("Shutdown ZJU-Connect success, Bye~")
Expand Down

0 comments on commit 7105140

Please sign in to comment.