This commit is contained in:
ICKelin
2024-04-24 20:59:10 +08:00
parent df5305f847
commit 1624c86924
8 changed files with 51 additions and 68 deletions

View File

@@ -9,6 +9,17 @@ import (
"github.com/ICKelin/gtun/src/internal/logs" "github.com/ICKelin/gtun/src/internal/logs"
) )
var logo = `
====================================
██████ ████████ ██ ██ ███ ██
██ ██ ██ ██ ████ ██
██ ███ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██
██████ ██ ██████ ██ ████
====================================
https://github.com/ICKelin/gtun
`
func main() { func main() {
flgConf := flag.String("c", "", "config file") flgConf := flag.String("c", "", "config file")
flag.Parse() flag.Parse()
@@ -18,8 +29,9 @@ func main() {
fmt.Printf("load config fail: %v\n", err) fmt.Printf("load config fail: %v\n", err)
return return
} }
fmt.Println(logo)
logs.Init(conf.Log.Path, conf.Log.Level, conf.Log.Days) logs.Init(conf.Log.Path, conf.Log.Level, conf.Log.Days)
logs.Info("%s", logo)
for region, cfg := range conf.Accelerator { for region, cfg := range conf.Accelerator {
err := route.Setup(region, cfg.Routes) err := route.Setup(region, cfg.Routes)
if err != nil { if err != nil {

View File

@@ -5,7 +5,6 @@ import (
"github.com/ICKelin/gtun/src/gtun/route" "github.com/ICKelin/gtun/src/gtun/route"
"github.com/ICKelin/gtun/src/internal/logs" "github.com/ICKelin/gtun/src/internal/logs"
"github.com/ICKelin/gtun/src/internal/proto" "github.com/ICKelin/gtun/src/internal/proto"
"github.com/ICKelin/gtun/src/internal/utils"
"io" "io"
"net" "net"
"sync" "sync"
@@ -26,7 +25,6 @@ type TProxyTCPConfig struct {
ReadTimeout int `json:"read_timeout"` ReadTimeout int `json:"read_timeout"`
WriteTimeout int `json:"write_timeout"` WriteTimeout int `json:"write_timeout"`
ListenAddr string `json:"listen_addr"` ListenAddr string `json:"listen_addr"`
RateLimit int `json:"rate_limit"`
} }
type TProxyTCP struct { type TProxyTCP struct {
@@ -43,8 +41,6 @@ type TProxyTCP struct {
mempool sync.Pool mempool sync.Pool
ratelimit *utils.RateLimit
routeManager *route.Manager routeManager *route.Manager
} }
@@ -83,9 +79,6 @@ func (p *TProxyTCP) initConfig(region string, cfg TProxyTCPConfig) error {
tcpWriteTimeout = defaultTCPTimeout tcpWriteTimeout = defaultTCPTimeout
} }
rateLimit := utils.NewRateLimit()
rateLimit.SetRateLimit(int64(cfg.RateLimit * 1024 * 1024))
p.region = region p.region = region
p.listenAddr = cfg.ListenAddr p.listenAddr = cfg.ListenAddr
p.writeTimeout = time.Duration(tcpWriteTimeout) * time.Second p.writeTimeout = time.Duration(tcpWriteTimeout) * time.Second
@@ -95,7 +88,6 @@ func (p *TProxyTCP) initConfig(region string, cfg TProxyTCPConfig) error {
return make([]byte, 32*1024) return make([]byte, 32*1024)
}, },
} }
p.ratelimit = rateLimit
p.routeManager = route.GetRouteManager() p.routeManager = route.GetRouteManager()
return nil return nil
} }
@@ -177,6 +169,8 @@ func (p *TProxyTCP) doProxy(conn net.Conn) {
io.CopyBuffer(stream, conn, buf) io.CopyBuffer(stream, conn, buf)
}() }()
defer stream.Close()
defer conn.Close()
obj := p.mempool.Get() obj := p.mempool.Get()
defer p.mempool.Put(obj) defer p.mempool.Put(obj)
buf := obj.([]byte) buf := obj.([]byte)

View File

@@ -40,7 +40,6 @@ type TProxyUDPConfig struct {
WriteTimeout int `json:"write_timeout"` WriteTimeout int `json:"write_timeout"`
SessionTimeout int `json:"session_timeout"` SessionTimeout int `json:"session_timeout"`
ListenAddr string `json:"listen_addr"` ListenAddr string `json:"listen_addr"`
RateLimit int `json:"rate_limit"`
} }
type TProxyUDP struct { type TProxyUDP struct {
@@ -57,8 +56,6 @@ type TProxyUDP struct {
// the purpose of udpSession is to reuse stream // the purpose of udpSession is to reuse stream
udpSessions map[string]*udpSession udpSessions map[string]*udpSession
udpsessLock sync.Mutex udpsessLock sync.Mutex
ratelimit *utils2.RateLimit
} }
func NewTProxyUDP() Proxy { func NewTProxyUDP() Proxy {
@@ -100,16 +97,12 @@ func (p *TProxyUDP) initConfig(region string, cfg TProxyUDPConfig) error {
sessionTimeout = defaultUDPSessionTimeout sessionTimeout = defaultUDPSessionTimeout
} }
rateLimit := utils2.NewRateLimit()
rateLimit.SetRateLimit(int64(cfg.RateLimit * 1024 * 1024))
p.region = region p.region = region
p.listenAddr = cfg.ListenAddr p.listenAddr = cfg.ListenAddr
p.writeTimeout = time.Duration(writeTimeout) * time.Second p.writeTimeout = time.Duration(writeTimeout) * time.Second
p.readTimeout = time.Duration(readTimeout) * time.Second p.readTimeout = time.Duration(readTimeout) * time.Second
p.sessionTimeout = time.Duration(sessionTimeout) * time.Second p.sessionTimeout = time.Duration(sessionTimeout) * time.Second
p.udpSessions = make(map[string]*udpSession) p.udpSessions = make(map[string]*udpSession)
p.ratelimit = rateLimit
p.routeManager = route.GetRouteManager() p.routeManager = route.GetRouteManager()
return nil return nil
} }

View File

@@ -117,7 +117,7 @@ func Setup(region string, routes []*config.RouteConfig) error {
for _, cfg := range routes { for _, cfg := range routes {
conn, err := newConn(region, cfg.Scheme, cfg.Server, cfg.AuthKey) conn, err := newConn(region, cfg.Scheme, cfg.Server, cfg.AuthKey)
if err != nil { if err != nil {
fmt.Printf("region[%s] connect to %s://%s fail: %v", fmt.Printf("region[%s] connect to %s://%s fail: %v\n",
region, cfg.Scheme, cfg.Server, cfg.AuthKey) region, cfg.Scheme, cfg.Server, cfg.AuthKey)
return err return err
} }

View File

@@ -8,6 +8,7 @@ import (
"net" "net"
"strings" "strings"
"testing" "testing"
"time"
) )
func TestRoute(t *testing.T) { func TestRoute(t *testing.T) {
@@ -71,6 +72,16 @@ func TestRoute(t *testing.T) {
type mockConn struct { type mockConn struct {
} }
func (m *mockConn) LocalAddr() net.Addr {
//TODO implement me
panic("implement me")
}
func (m *mockConn) SetDeadline(t time.Time) error {
//TODO implement me
panic("implement me")
}
func (m *mockConn) OpenStream() (transport.Stream, error) { func (m *mockConn) OpenStream() (transport.Stream, error) {
//TODO implement me //TODO implement me
panic("implement me") panic("implement me")

View File

@@ -26,6 +26,7 @@ type Log struct {
type AuthConfig struct { type AuthConfig struct {
AccessToken string `yaml:"access_token"` AccessToken string `yaml:"access_token"`
ExpiredAt int64 `yaml:"expired_at"` ExpiredAt int64 `yaml:"expired_at"`
RateLimit int64 `yaml:"rate_limit"` // mbps
} }
func ParseConfig(path string) (*Config, error) { func ParseConfig(path string) (*Config, error) {

View File

@@ -15,27 +15,49 @@ import (
var version = "" var version = ""
var logo = `
====================================
██████ ████████ ██ ██ ███ ██
██ ██ ██ ██ ████ ██
██ ███ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██
██████ ██ ██████ ██ ████
====================================
https://github.com/ICKelin/gtun
`
func init() { func init() {
go http.ListenAndServe(":6060", nil) go http.ListenAndServe(":6060", nil)
} }
func main() { func main() {
flgVersion := flag.Bool("v", false, "print version") flgVersion := flag.Bool("v", false, "print version")
flgTest := flag.Bool("t", false, "test config file")
flgConf := flag.String("c", "", "config file") flgConf := flag.String("c", "", "config file")
flag.Parse() flag.Parse()
fmt.Println(logo)
if *flgVersion { if *flgVersion {
fmt.Println(version) fmt.Println(version)
return return
} }
if *flgTest {
_, err := ParseConfig(*flgConf)
if err != nil {
fmt.Printf("FAILED: %v\n", err)
}
return
}
conf, err := ParseConfig(*flgConf) conf, err := ParseConfig(*flgConf)
if err != nil { if err != nil {
fmt.Printf("parse config file fail: %s %v\n", *flgConf, err) fmt.Printf("parse config file fail: %s %v\n", *flgConf, err)
return return
} }
logs.Init(conf.Log.Path, conf.Log.Level, conf.Log.Days) logs.Init(conf.Log.Path, conf.Log.Level, conf.Log.Days)
logs.Info("%s", logo)
logs.Debug("config: %s", conf.String()) logs.Debug("config: %s", conf.String())
if conf.Trace != "" { if conf.Trace != "" {

View File

@@ -116,15 +116,6 @@ func (s *Server) handleStream(stream transport.Stream) {
s.tcpProxy(stream, &proxyProtocol) s.tcpProxy(stream, &proxyProtocol)
case "udp": case "udp":
s.udpProxy(stream, &proxyProtocol) s.udpProxy(stream, &proxyProtocol)
case "tun":
if s.devStream != nil {
s.devStream.Close()
s.devStream = stream
} else {
s.devStream = stream
go s.readDev()
}
s.tunProxy(stream, &proxyProtocol)
} }
} }
@@ -220,44 +211,3 @@ func (s *Server) udpProxy(stream transport.Stream, p *proto.ProxyProtocol) {
} }
} }
} }
func (s *Server) tunProxy(stream transport.Stream, p *proto.ProxyProtocol) {
defer stream.Close()
hdr := make([]byte, 2)
for {
_, err := io.ReadFull(stream, hdr)
if err != nil {
logs.Error("read stream fail: %v", err)
break
}
nlen := binary.BigEndian.Uint16(hdr)
buf := make([]byte, nlen)
_, err = io.ReadFull(stream, buf)
if err != nil {
logs.Error("read stream body fail: %v", err)
break
}
_, err = s.dev.Write(buf)
if err != nil {
logs.Error("write to dev fail: %v", err)
return
}
}
}
func (s *Server) readDev() {
for {
buf, err := s.dev.Read()
if err != nil {
logs.Error("read interface fail: %v", err)
return
}
bytes := proto.EncodeData(buf)
_, err = s.devStream.Write(bytes)
if err != nil {
logs.Error("stream write fail: %v", err)
}
}
}