Browse Source

update: xproxy init process

v1.x.x
dnomd343 2 years ago
parent
commit
f40885765d
  1. 5
      Dockerfile
  2. 11
      cmd/asset/update.go
  3. 45
      cmd/controller.go
  4. 4
      cmd/network/main.go
  5. 32
      cmd/xproxy.go
  6. 48
      main.go
  7. 28
      src/main.go

5
Dockerfile

@ -14,8 +14,7 @@ RUN wget https://github.com/XTLS/Xray-core/archive/refs/tags/v${XRAY_VERSION}.ta
tar xf v${XRAY_VERSION}.tar.gz
WORKDIR ./Xray-core-${XRAY_VERSION}/
RUN go mod download -x
RUN env CGO_ENABLED=0 go build -v -o xray -trimpath -ldflags "-s -w" ./main && \
mv ./xray /tmp/
RUN env CGO_ENABLED=0 go build -v -o xray -trimpath -ldflags "-s -w" ./main && mv ./xray /tmp/
COPY --from=upx /upx/ /usr/
RUN upx -9 /tmp/xray
@ -24,7 +23,7 @@ RUN apk add xz
WORKDIR /tmp/
RUN wget "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat"
RUN wget "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat"
RUN mkdir -p /asset/etc/xproxy/ && tar cJf /asset/etc/xproxy/assets.tar.xz ./geo*.dat
RUN mkdir -p /asset/ && tar cJf /asset/assets.tar.xz ./*.dat
COPY --from=xray /tmp/xray /asset/usr/bin/
FROM alpine:3.16

11
cmd/asset/update.go

@ -2,10 +2,11 @@ package asset
import (
"XProxy/cmd/common"
"github.com/robfig/cron"
log "github.com/sirupsen/logrus"
)
func UpdateAssets(urls map[string]string, assetDir string) {
func updateAssets(urls map[string]string, assetDir string) {
if len(urls) != 0 {
log.Info("Start update assets")
for file, url := range urls {
@ -13,3 +14,11 @@ func UpdateAssets(urls map[string]string, assetDir string) {
}
}
}
func AutoUpdate(updateCron string, updateUrls map[string]string, assetDir string) {
autoUpdate := cron.New()
_ = autoUpdate.AddFunc(updateCron, func() {
updateAssets(updateUrls, assetDir)
})
autoUpdate.Start()
}

45
cmd/controller.go

@ -0,0 +1,45 @@
package main
import (
"XProxy/cmd/asset"
"XProxy/cmd/config"
"XProxy/cmd/network"
"XProxy/cmd/proxy"
)
func loadNetwork(settings *config.Config) {
v4Settings := network.Config{
RouteTable: v4RouteTable,
TProxyPort: v4TProxyPort,
Address: settings.V4Address,
Gateway: settings.V4Gateway,
Bypass: settings.V4Bypass,
}
v6Settings := network.Config{
RouteTable: v6RouteTable,
TProxyPort: v6TProxyPort,
Address: settings.V6Address,
Gateway: settings.V6Gateway,
Bypass: settings.V6Bypass,
}
network.Load(settings.DNS, v4Settings, v6Settings)
}
func loadProxy(settings *config.Config) {
proxy.Load(configDir, exposeDir, proxy.Config{
Sniff: settings.EnableSniff,
Redirect: settings.EnableRedirect,
V4TProxyPort: v4TProxyPort,
V6TProxyPort: v6TProxyPort,
LogLevel: "warning",
HttpInbounds: settings.HttpInbounds,
SocksInbounds: settings.SocksInbounds,
AddOnInbounds: settings.AddOnInbounds,
})
}
func loadAsset(settings *config.Config) {
asset.LoadGeoSite(assetFile, assetDir)
asset.LoadGeoIp(assetFile, assetDir)
asset.AutoUpdate(settings.UpdateCron, settings.UpdateUrls, assetDir)
}

4
cmd/network/main.go

@ -11,12 +11,10 @@ type Config struct {
}
func Load(dns []string, ipv4 Config, ipv6 Config) {
loadDns(dns) // init dns server
loadDns(dns) // init dns server
flushNetwork() // clear network settings
loadV4Network(ipv4)
loadV6Network(ipv6)
time.Sleep(time.Second) // wait 1s for ipv6 (ND protocol)
loadV4TProxy(ipv4, getV4Cidr())
loadV6TProxy(ipv6, getV6Cidr())

32
cmd/xproxy.go

@ -0,0 +1,32 @@
package main
import (
"XProxy/cmd/config"
"fmt"
log "github.com/sirupsen/logrus"
)
var version = "dev"
var v4RouteTable = 100
var v6RouteTable = 106
var v4TProxyPort = 7288
var v6TProxyPort = 7289
var exposeDir = "/xproxy"
var configDir = "/etc/xproxy"
var assetFile = "/assets.tar.xz"
var assetDir = exposeDir + "/assets"
var configFile = exposeDir + "/config.yml"
func main() {
log.SetLevel(log.DebugLevel)
fmt.Println("XProxy start -> version =", version)
settings := config.Load(configFile)
loadNetwork(&settings)
loadProxy(&settings)
loadAsset(&settings)
// TODO: start xray service
}

48
main.go

@ -1,48 +0,0 @@
package main
import (
"XProxy/cmd/config"
"XProxy/cmd/network"
"fmt"
log "github.com/sirupsen/logrus"
)
var exposeDir = "/xproxy"
var v4RouteTable = 100
var v6RouteTable = 106
var v4TProxyPort = 7288
var v6TProxyPort = 7289
func loadNetwork(settings *config.Config) {
v4Settings := network.Config{
RouteTable: v4RouteTable,
TProxyPort: v4TProxyPort,
Address: settings.V4Address,
Gateway: settings.V4Gateway,
Bypass: settings.V4Bypass,
}
v6Settings := network.Config{
RouteTable: v6RouteTable,
TProxyPort: v6TProxyPort,
Address: settings.V6Address,
Gateway: settings.V6Gateway,
Bypass: settings.V6Bypass,
}
network.Load(settings.DNS, v4Settings, v6Settings)
}
func main() {
log.SetLevel(log.DebugLevel)
fmt.Println("XProxy start")
settings := config.Load(exposeDir + "/config.yml")
fmt.Println(settings)
loadNetwork(&settings)
// TODO: load proxy
// TODO: load asset
// TODO: start xray service
}

28
src/main.go

@ -1,26 +1,13 @@
package main
import (
"github.com/robfig/cron"
log "github.com/sirupsen/logrus"
"os"
"os/signal"
"syscall"
)
var logLevel = "warning"
var preScript []string
var updateCron string
var updateUrls map[string]string
var enableSniff bool
var enableRedirect bool
var httpInbounds map[string]int
var socksInbounds map[string]int
var addOnInbounds []interface{}
var assetFile = "/etc/xproxy/assets.tar.xz"
func main() {
defer func() {
@ -29,21 +16,6 @@ func main() {
}
}()
log.SetLevel(log.DebugLevel)
log.Warning("XProxy start")
loadConfig("/xproxy/config.yml")
loadProxy("/etc/xproxy/config", "/xproxy")
loadGeoSite("/xproxy/assets")
loadGeoIp("/xproxy/assets")
autoUpdate := cron.New()
_ = autoUpdate.AddFunc(updateCron, func() {
updateAssets("/xproxy/assets")
})
autoUpdate.Start()
loadDns()
loadNetwork()
loadTProxy()
for _, script := range preScript {
log.Infof("Run script command -> %s", script)
runCommand("sh", "-c", script)

Loading…
Cancel
Save