diff --git a/src/load.go b/src/load.go index 53e592b..3936698 100644 --- a/src/load.go +++ b/src/load.go @@ -2,7 +2,6 @@ package main import ( "encoding/json" - "fmt" log "github.com/sirupsen/logrus" "io" "io/ioutil" @@ -49,23 +48,6 @@ var outboundsConfig = `{ ] }` -type emptySettings struct{} - -type socksSettings struct { - UDP bool `json:"udp"` -} - -type tproxySettings struct { - Network string `json:"network"` - FollowRedirect bool `json:"followRedirect"` -} - -type tproxyStreamSettings struct { - Sockopt struct { - Tproxy string `json:"tproxy"` - } `json:"sockopt"` -} - type sniffSettings struct { Enabled bool `json:"enabled"` RouteOnly bool `json:"routeOnly"` @@ -81,6 +63,10 @@ type inboundSettings struct { Sniffing sniffSettings `json:"sniffing"` } +type inboundsSettings struct { + Inbounds []interface{} `json:"inbounds"` +} + func isFileExist(filePath string) bool { s, err := os.Stat(filePath) if err != nil { // file or folder not exist @@ -146,6 +132,58 @@ func saveConfig(configDir string, caption string, content string, overwrite bool } } +func loadHttpProxy(tag string, port int, sniffObject sniffSettings) interface{} { + type empty struct{} + return inboundSettings{ + Tag: tag, + Port: port, + Protocol: "http", + Settings: empty{}, + StreamSettings: empty{}, + Sniffing: sniffObject, + } +} + +func loadSocksProxy(tag string, port int, sniffObject sniffSettings) interface{} { + type empty struct{} + type socksSettings struct { + UDP bool `json:"udp"` + } + return inboundSettings{ + Tag: tag, + Port: port, + Protocol: "socks", + Settings: socksSettings{UDP: true}, + StreamSettings: empty{}, + Sniffing: sniffObject, + } +} + +func loadTProxy(tag string, port int, sniffObject sniffSettings) interface{} { + type tproxySettings struct { + Network string `json:"network"` + FollowRedirect bool `json:"followRedirect"` + } + type tproxyStreamSettings struct { + Sockopt struct { + Tproxy string `json:"tproxy"` + } `json:"sockopt"` + } + tproxyStream := tproxyStreamSettings{} + tproxyStream.Sockopt.Tproxy = "tproxy" + return inboundSettings{ + Tag: tag, + Port: port, + Protocol: "dokodemo-door", + Settings: tproxySettings{ + Network: "tcp,udp", + FollowRedirect: true, + }, + StreamSettings: tproxyStream, + Sniffing: sniffObject, + } +} + func loadProxy(configDir string, exposeDir string) { createFolder(exposeDir + "/log") createFolder(exposeDir + "/config") @@ -158,54 +196,22 @@ func loadProxy(configDir string, exposeDir string) { logConfig = strings.ReplaceAll(logConfig, "${DIR}", exposeDir+"/log") saveConfig(configDir, "log", logConfig+"\n", true) - // TODO: load inbounds config - + inboundsObject := inboundsSettings{} sniffObject := sniffSettings{ Enabled: enableSniff, RouteOnly: !enableRedirect, DestOverride: []string{"http", "tls"}, } - - httpObject := inboundSettings{ - Tag: "123", - Port: 1234, - Protocol: "http", - Settings: emptySettings{}, - StreamSettings: emptySettings{}, - Sniffing: sniffObject, - } - - b, _ := json.Marshal(httpObject) - fmt.Println(string(b)) - - socksObject := inboundSettings{ - Tag: "123", - Port: 2345, - Protocol: "socks", - Settings: socksSettings{ - UDP: true, - }, - StreamSettings: emptySettings{}, - Sniffing: sniffObject, + inboundsObject.Inbounds = append(inboundsObject.Inbounds, loadTProxy("tproxy", v4TProxyPort, sniffObject)) + inboundsObject.Inbounds = append(inboundsObject.Inbounds, loadTProxy("tproxy6", v6TProxyPort, sniffObject)) + for tag, port := range httpInbounds { + inboundsObject.Inbounds = append(inboundsObject.Inbounds, loadHttpProxy(tag, port, sniffObject)) } - - b, _ = json.Marshal(socksObject) - fmt.Println(string(b)) - - tproxyObject := inboundSettings{ - Tag: "123", - Port: 7288, - Protocol: "dokodemo-door", - Settings: tproxySettings{ - Network: "tcp,udp", - FollowRedirect: true, - }, - StreamSettings: emptySettings{}, - Sniffing: sniffObject, + for tag, port := range socksInbounds { + inboundsObject.Inbounds = append(inboundsObject.Inbounds, loadSocksProxy(tag, port, sniffObject)) } - - b, _ = json.Marshal(tproxyObject) - fmt.Println(string(b)) + inboundsConfig, _ := json.MarshalIndent(inboundsObject, "", " ") // json encode + saveConfig(configDir, "inbounds", string(inboundsConfig), true) for _, configFile := range listFolder(exposeDir+"/config", ".json") { if configFile == "log.json" || configFile == "inbounds.json" { diff --git a/src/main.go b/src/main.go index af96075..9e04122 100644 --- a/src/main.go +++ b/src/main.go @@ -1,6 +1,7 @@ package main import ( + "fmt" log "github.com/sirupsen/logrus" ) @@ -11,10 +12,22 @@ var v6TProxyPort = 7289 var enableSniff = false var enableRedirect = true +var httpInbounds = make(map[string]int) +var socksInbounds = make(map[string]int) + func main() { log.SetLevel(log.DebugLevel) log.Warning("XProxy start") + httpInbounds["ipv4"] = 1084 + httpInbounds["ipv6"] = 1086 + fmt.Println(httpInbounds) + + socksInbounds["nodeA"] = 1681 + socksInbounds["nodeB"] = 1682 + socksInbounds["nodeC"] = 1683 + fmt.Println(socksInbounds) + loadProxy("/etc/xproxy/config", "/xproxy") //content, err := os.ReadFile("test.yml")