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 PROXY=direct://
ENV MTU=9000
ENV STATS=
ENV TOKEN=
ENV RESTAPI=
ENV UDP_TIMEOUT=
ENV EXTRA_COMMANDS=
ENV TUN_INCLUDED_ROUTES=

View File

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

View File

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

View File

@@ -3,6 +3,7 @@ package engine
import (
"encoding/base64"
"fmt"
"net"
"net/url"
"strings"
@@ -13,6 +14,33 @@ import (
"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) {
if !strings.Contains(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.LogLevel, "loglevel", "info", "Log level [debug|info|warning|error|silent]")
flag.StringVar(&key.Proxy, "proxy", "", "Use this proxy [protocol://]host[:port]")
flag.StringVar(&key.Stats, "stats", "", "HTTP statistic server listen address")
flag.StringVar(&key.Token, "token", "", "HTTP statistic server auth token")
flag.StringVar(&key.RestAPI, "restapi", "", "HTTP statistic server listen address")
flag.Parse()
}

View File

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

View File

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

View File

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

View File

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

View File

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