Browse Source

update: inbounds config of xray

v1.x.x
dnomd343 2 years ago
parent
commit
ec4f1f25ea
  1. 124
      src/load.go
  2. 13
      src/main.go

124
src/load.go

@ -2,7 +2,6 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"io" "io"
"io/ioutil" "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 { type sniffSettings struct {
Enabled bool `json:"enabled"` Enabled bool `json:"enabled"`
RouteOnly bool `json:"routeOnly"` RouteOnly bool `json:"routeOnly"`
@ -81,6 +63,10 @@ type inboundSettings struct {
Sniffing sniffSettings `json:"sniffing"` Sniffing sniffSettings `json:"sniffing"`
} }
type inboundsSettings struct {
Inbounds []interface{} `json:"inbounds"`
}
func isFileExist(filePath string) bool { func isFileExist(filePath string) bool {
s, err := os.Stat(filePath) s, err := os.Stat(filePath)
if err != nil { // file or folder not exist 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) { func loadProxy(configDir string, exposeDir string) {
createFolder(exposeDir + "/log") createFolder(exposeDir + "/log")
createFolder(exposeDir + "/config") createFolder(exposeDir + "/config")
@ -158,54 +196,22 @@ func loadProxy(configDir string, exposeDir string) {
logConfig = strings.ReplaceAll(logConfig, "${DIR}", exposeDir+"/log") logConfig = strings.ReplaceAll(logConfig, "${DIR}", exposeDir+"/log")
saveConfig(configDir, "log", logConfig+"\n", true) saveConfig(configDir, "log", logConfig+"\n", true)
// TODO: load inbounds config inboundsObject := inboundsSettings{}
sniffObject := sniffSettings{ sniffObject := sniffSettings{
Enabled: enableSniff, Enabled: enableSniff,
RouteOnly: !enableRedirect, RouteOnly: !enableRedirect,
DestOverride: []string{"http", "tls"}, DestOverride: []string{"http", "tls"},
} }
inboundsObject.Inbounds = append(inboundsObject.Inbounds, loadTProxy("tproxy", v4TProxyPort, sniffObject))
httpObject := inboundSettings{ inboundsObject.Inbounds = append(inboundsObject.Inbounds, loadTProxy("tproxy6", v6TProxyPort, sniffObject))
Tag: "123", for tag, port := range httpInbounds {
Port: 1234, inboundsObject.Inbounds = append(inboundsObject.Inbounds, loadHttpProxy(tag, port, sniffObject))
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,
} }
for tag, port := range socksInbounds {
b, _ = json.Marshal(socksObject) inboundsObject.Inbounds = append(inboundsObject.Inbounds, loadSocksProxy(tag, port, sniffObject))
fmt.Println(string(b))
tproxyObject := inboundSettings{
Tag: "123",
Port: 7288,
Protocol: "dokodemo-door",
Settings: tproxySettings{
Network: "tcp,udp",
FollowRedirect: true,
},
StreamSettings: emptySettings{},
Sniffing: sniffObject,
} }
inboundsConfig, _ := json.MarshalIndent(inboundsObject, "", " ") // json encode
b, _ = json.Marshal(tproxyObject) saveConfig(configDir, "inbounds", string(inboundsConfig), true)
fmt.Println(string(b))
for _, configFile := range listFolder(exposeDir+"/config", ".json") { for _, configFile := range listFolder(exposeDir+"/config", ".json") {
if configFile == "log.json" || configFile == "inbounds.json" { if configFile == "log.json" || configFile == "inbounds.json" {

13
src/main.go

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -11,10 +12,22 @@ var v6TProxyPort = 7289
var enableSniff = false var enableSniff = false
var enableRedirect = true var enableRedirect = true
var httpInbounds = make(map[string]int)
var socksInbounds = make(map[string]int)
func main() { func main() {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
log.Warning("XProxy start") 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") loadProxy("/etc/xproxy/config", "/xproxy")
//content, err := os.ReadFile("test.yml") //content, err := os.ReadFile("test.yml")

Loading…
Cancel
Save