Refactor: stats -> restapi

This commit is contained in:
xjasonlyu
2022-03-29 19:25:45 +08:00
parent e6911cb6fb
commit b166ed5e66
11 changed files with 50 additions and 36 deletions

View File

@@ -20,8 +20,7 @@ ENV ADDR=198.18.0.1/15
ENV LOGLEVEL=info ENV LOGLEVEL=info
ENV PROXY=direct:// ENV PROXY=direct://
ENV MTU=9000 ENV MTU=9000
ENV STATS= ENV RESTAPI=
ENV TOKEN=
ENV UDP_TIMEOUT= ENV UDP_TIMEOUT=
ENV EXTRA_COMMANDS= ENV EXTRA_COMMANDS=
ENV TUN_INCLUDED_ROUTES= ENV TUN_INCLUDED_ROUTES=

View File

@@ -61,12 +61,8 @@ run() {
ARGS="--mtu $MTU" ARGS="--mtu $MTU"
fi fi
if [ -n "$STATS" ]; then if [ -n "$RESTAPI" ]; then
ARGS="$ARGS --stats $STATS" ARGS="$ARGS --restapi $RESTAPI"
fi
if [ -n "$TOKEN" ]; then
ARGS="$ARGS --token $TOKEN"
fi fi
if [ -n "$UDP_TIMEOUT" ]; then if [ -n "$UDP_TIMEOUT" ]; then

View File

@@ -2,7 +2,6 @@ package engine
import ( import (
"errors" "errors"
"net"
"github.com/xjasonlyu/tun2socks/v2/component/dialer" "github.com/xjasonlyu/tun2socks/v2/component/dialer"
"github.com/xjasonlyu/tun2socks/v2/core" "github.com/xjasonlyu/tun2socks/v2/core"
@@ -10,7 +9,7 @@ import (
_ "github.com/xjasonlyu/tun2socks/v2/dns" _ "github.com/xjasonlyu/tun2socks/v2/dns"
"github.com/xjasonlyu/tun2socks/v2/log" "github.com/xjasonlyu/tun2socks/v2/log"
"github.com/xjasonlyu/tun2socks/v2/proxy" "github.com/xjasonlyu/tun2socks/v2/proxy"
"github.com/xjasonlyu/tun2socks/v2/stats" "github.com/xjasonlyu/tun2socks/v2/restapi"
"github.com/xjasonlyu/tun2socks/v2/tunnel" "github.com/xjasonlyu/tun2socks/v2/tunnel"
"gvisor.dev/gvisor/pkg/tcpip" "gvisor.dev/gvisor/pkg/tcpip"
@@ -43,8 +42,7 @@ type Key struct {
Mark int `yaml:"fwmark"` Mark int `yaml:"fwmark"`
UDPTimeout int `yaml:"udp-timeout"` UDPTimeout int `yaml:"udp-timeout"`
Proxy string `yaml:"proxy"` Proxy string `yaml:"proxy"`
Stats string `yaml:"stats"` RestAPI string `yaml:"restapi"`
Token string `yaml:"token"`
Device string `yaml:"device"` Device string `yaml:"device"`
LogLevel string `yaml:"loglevel"` LogLevel string `yaml:"loglevel"`
Interface string `yaml:"interface"` Interface string `yaml:"interface"`
@@ -66,7 +64,7 @@ func (e *engine) start() error {
for _, f := range []func() error{ for _, f := range []func() error{
e.applyLogLevel, e.applyLogLevel,
e.applyDialer, e.applyDialer,
e.applyStats, e.applyRestAPI,
e.applyUDPTimeout, e.applyUDPTimeout,
e.applyProxy, e.applyProxy,
e.applyDevice, e.applyDevice,
@@ -115,19 +113,20 @@ func (e *engine) applyDialer() error {
return nil return nil
} }
func (e *engine) applyStats() error { func (e *engine) applyRestAPI() error {
if e.Stats != "" { if e.RestAPI != "" {
addr, err := net.ResolveTCPAddr("tcp", e.Stats) u, err := parseRestAPI(e.RestAPI)
if err != nil { if err != nil {
return err return err
} }
host, token := u.Host, u.User.String()
go func() { go func() {
if err := stats.Start(addr.String(), e.Token); err != nil { if err := restapi.Start(host, token); err != nil {
log.Warnf("[STATS] failed to start: %v", err) log.Warnf("[RESTAPI] failed to start: %v", err)
} }
}() }()
log.Infof("[STATS] serve at: http://%s", addr) log.Infof("[RESTAPI] serve at: %s", u)
} }
return nil return nil
} }

View File

@@ -3,6 +3,7 @@ package engine
import ( import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"net"
"net/url" "net/url"
"strings" "strings"
@@ -13,6 +14,33 @@ import (
"github.com/xjasonlyu/tun2socks/v2/proxy/proto" "github.com/xjasonlyu/tun2socks/v2/proxy/proto"
) )
func parseRestAPI(s string) (*url.URL, error) {
if !strings.Contains(s, "://") {
s = fmt.Sprintf("%s://%s", "http", s)
}
u, err := url.Parse(s)
if err != nil {
return nil, err
}
addr, err := net.ResolveTCPAddr("tcp", u.Host)
if err != nil {
return nil, err
}
if addr.IP == nil {
addr.IP = net.IPv4zero /* default: 0.0.0.0 */
}
u.Host = addr.String()
switch u.Scheme {
case "http":
return u, nil
default:
return nil, fmt.Errorf("unsupported scheme: %s", u.Scheme)
}
}
func parseDevice(s string, mtu uint32) (device.Device, error) { func parseDevice(s string, mtu uint32) (device.Device, error) {
if !strings.Contains(s, "://") { if !strings.Contains(s, "://") {
s = fmt.Sprintf("%s://%s", tun.Driver /* default driver */, s) s = fmt.Sprintf("%s://%s", tun.Driver /* default driver */, s)

View File

@@ -32,8 +32,7 @@ func init() {
flag.StringVar(&key.Interface, "interface", "", "Use network INTERFACE (Linux/MacOS only)") flag.StringVar(&key.Interface, "interface", "", "Use network INTERFACE (Linux/MacOS only)")
flag.StringVar(&key.LogLevel, "loglevel", "info", "Log level [debug|info|warning|error|silent]") flag.StringVar(&key.LogLevel, "loglevel", "info", "Log level [debug|info|warning|error|silent]")
flag.StringVar(&key.Proxy, "proxy", "", "Use this proxy [protocol://]host[:port]") flag.StringVar(&key.Proxy, "proxy", "", "Use this proxy [protocol://]host[:port]")
flag.StringVar(&key.Stats, "stats", "", "HTTP statistic server listen address") flag.StringVar(&key.RestAPI, "restapi", "", "HTTP statistic server listen address")
flag.StringVar(&key.Token, "token", "", "HTTP statistic server auth token")
flag.Parse() flag.Parse()
} }

View File

@@ -1,4 +1,4 @@
package stats package restapi
import ( import (
"bytes" "bytes"

View File

@@ -1,6 +1,6 @@
//go:build debug //go:build debug
package stats package restapi
import ( import (
"net/http" "net/http"

View File

@@ -1,4 +1,4 @@
package stats package restapi
var ( var (
ErrUnauthorized = newError("Unauthorized") ErrUnauthorized = newError("Unauthorized")

View File

@@ -1,4 +1,4 @@
package stats package restapi
// TODO: Network statistic support. // TODO: Network statistic support.
func init() {} func init() {}

View File

@@ -1,4 +1,6 @@
package stats package restapi
// Ref: github.com/Dreamacro/clash/hub/route
import ( import (
"bytes" "bytes"
@@ -55,12 +57,7 @@ func Start(addr, token string) error {
} }
}) })
tcpAddr, err := net.ResolveTCPAddr("tcp", addr) listener, err := net.Listen("tcp", addr)
if err != nil {
return err
}
listener, err := net.ListenTCP("tcp", tcpAddr)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -1,4 +0,0 @@
// Package stats provides statistic data via http server.
package stats
// Ref: github.com/Dreamacro/clash/hub/route