update
parent
e4c9049924
commit
1b27f3b2ab
|
@ -1,14 +0,0 @@
|
||||||
package app
|
|
||||||
|
|
||||||
type (
|
|
||||||
config struct {
|
|
||||||
Proxy map[string]ProxyItem
|
|
||||||
}
|
|
||||||
|
|
||||||
ProxyItem struct {
|
|
||||||
RemoteAddr []string
|
|
||||||
LocalAddr string
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
var Conf = &config{}
|
|
39
app/proxy.go
39
app/proxy.go
|
@ -2,7 +2,7 @@ package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"git.makemake.in/kzkzzzz/mycommon/myconf"
|
||||||
"git.makemake.in/kzkzzzz/mycommon/mylog"
|
"git.makemake.in/kzkzzzz/mycommon/mylog"
|
||||||
"io"
|
"io"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
@ -14,14 +14,32 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ProxyItem struct {
|
||||||
|
RemoteAddr []string
|
||||||
|
LocalAddr string
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
mainWg = &sync.WaitGroup{}
|
mainWg = &sync.WaitGroup{}
|
||||||
mainCtx, mainCancel = context.WithCancel(context.Background())
|
mainCtx, mainCancel = context.WithCancel(context.Background())
|
||||||
|
|
||||||
|
proxyInfo map[string]*ProxyItem
|
||||||
)
|
)
|
||||||
|
|
||||||
func Run() {
|
func Run() {
|
||||||
for name, item := range Conf.Proxy {
|
err := myconf.Conf().UnmarshalKey("proxy", &proxyInfo)
|
||||||
go listenTcp(name, item)
|
if err != nil {
|
||||||
|
mylog.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
names := make([]string, 0, len(proxyInfo))
|
||||||
|
for name := range proxyInfo {
|
||||||
|
names = append(names, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, name := range names {
|
||||||
|
go listenTcp(name, proxyInfo[name])
|
||||||
}
|
}
|
||||||
|
|
||||||
ch := make(chan os.Signal, 1)
|
ch := make(chan os.Signal, 1)
|
||||||
|
@ -36,15 +54,14 @@ func Run() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func listenTcp(name string, item ProxyItem) {
|
func listenTcp(name string, item *ProxyItem) {
|
||||||
fmt.Printf("%+v\n", item.LocalAddr)
|
listen, err := net.Listen("tcp4", item.LocalAddr)
|
||||||
listen, err := net.Listen("tcp", item.LocalAddr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mylog.Errorf("[%s] listen err: %s", name, err)
|
mylog.Errorf("[%s] listen err: %s", name, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
mylog.Infof("[%s] listen %s", name, item.LocalAddr)
|
mylog.Infof("[%s] forward %s -> %+v", name, item.LocalAddr, item.RemoteAddr)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
localConn, err := listen.Accept()
|
localConn, err := listen.Accept()
|
||||||
|
@ -58,7 +75,7 @@ func listenTcp(name string, item ProxyItem) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func proxyTcp(localConn net.Conn, item ProxyItem) {
|
func proxyTcp(localConn net.Conn, item *ProxyItem) {
|
||||||
defer mainWg.Done()
|
defer mainWg.Done()
|
||||||
|
|
||||||
randRemoteAddr := item.RemoteAddr[rand.Intn(len(item.RemoteAddr))]
|
randRemoteAddr := item.RemoteAddr[rand.Intn(len(item.RemoteAddr))]
|
||||||
|
@ -70,7 +87,7 @@ func proxyTcp(localConn net.Conn, item ProxyItem) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
mylog.Infof("start proxy %s -> %s", localConn.RemoteAddr(), randRemoteAddr)
|
mylog.Debugf("start proxy %s -> %s", localConn.RemoteAddr(), randRemoteAddr)
|
||||||
|
|
||||||
waitCh := make(chan struct{})
|
waitCh := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -90,7 +107,7 @@ func proxyTcp(localConn net.Conn, item ProxyItem) {
|
||||||
remoteConn.Close()
|
remoteConn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
mylog.Infof("stop proxy %s -> %s", localConn.RemoteAddr(), randRemoteAddr)
|
mylog.Debugf("stop proxy %s -> %s", localConn.RemoteAddr(), randRemoteAddr)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,7 +120,7 @@ func copyConn(wg0 *sync.WaitGroup, localConn, remoteConn net.Conn) {
|
||||||
|
|
||||||
_, err := io.Copy(remoteConn, localConn)
|
_, err := io.Copy(remoteConn, localConn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mylog.Error(err)
|
mylog.Debug(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
25
go.mod
25
go.mod
|
@ -3,28 +3,29 @@ module proxyport
|
||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.makemake.in/kzkzzzz/mycommon v0.0.0-20240623062425-ebf5c21cde3a
|
git.makemake.in/kzkzzzz/mycommon v0.0.0-20240719075030-85d75101130c
|
||||||
github.com/spf13/pflag v1.0.5
|
github.com/spf13/pflag v1.0.5
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/fsnotify/fsnotify v1.6.0 // indirect
|
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||||
github.com/magiconair/properties v1.8.7 // indirect
|
github.com/magiconair/properties v1.8.7 // indirect
|
||||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||||
github.com/pelletier/go-toml v1.9.5 // indirect
|
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
|
github.com/sagikazarmark/locafero v0.4.0 // indirect
|
||||||
github.com/spf13/afero v1.9.3 // indirect
|
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
|
||||||
github.com/spf13/cast v1.5.0 // indirect
|
github.com/sourcegraph/conc v0.3.0 // indirect
|
||||||
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
github.com/spf13/afero v1.11.0 // indirect
|
||||||
github.com/spf13/viper v1.14.0 // indirect
|
github.com/spf13/cast v1.6.0 // indirect
|
||||||
github.com/subosito/gotenv v1.4.2 // indirect
|
github.com/spf13/viper v1.19.0 // indirect
|
||||||
|
github.com/subosito/gotenv v1.6.0 // indirect
|
||||||
go.uber.org/multierr v1.11.0 // indirect
|
go.uber.org/multierr v1.11.0 // indirect
|
||||||
go.uber.org/zap v1.27.0 // indirect
|
go.uber.org/zap v1.27.0 // indirect
|
||||||
golang.org/x/sys v0.4.0 // indirect
|
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
|
||||||
golang.org/x/text v0.6.0 // indirect
|
golang.org/x/sys v0.18.0 // indirect
|
||||||
|
golang.org/x/text v0.14.0 // indirect
|
||||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
|
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
|
||||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|
12
main.go
12
main.go
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"git.makemake.in/kzkzzzz/mycommon/myconf"
|
"git.makemake.in/kzkzzzz/mycommon/myconf"
|
||||||
"git.makemake.in/kzkzzzz/mycommon/mylog"
|
"git.makemake.in/kzkzzzz/mycommon/mylog"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
|
@ -10,16 +9,15 @@ import (
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
pflag.String("conf", "config.toml", "config file path")
|
pflag.String("conf", "config.toml", "config file path")
|
||||||
|
pflag.String("log.level", "info", "log level")
|
||||||
myconf.LoadFlag()
|
myconf.LoadFlag()
|
||||||
|
|
||||||
cfg := mylog.DefaultConfig
|
myconf.LoadFile(myconf.Conf().GetString("conf"))
|
||||||
cfg.ConsoleWriter = nil
|
|
||||||
cfg.NeedLogFile = true
|
|
||||||
mylog.Init("debug", cfg)
|
|
||||||
|
|
||||||
myconf.LoadFileTo(myconf.Conf().GetString("conf"), &app.Conf)
|
config := mylog.DefaultConfig
|
||||||
|
config.Level = myconf.Conf().GetString("log.level")
|
||||||
|
mylog.Init("", config)
|
||||||
defer mylog.Flush()
|
defer mylog.Flush()
|
||||||
|
|
||||||
fmt.Printf("%+v\n", app.Conf)
|
|
||||||
app.Run()
|
app.Run()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue