Skip to content

Commit

Permalink
Refactor: stats -> restapi
Browse files Browse the repository at this point in the history
  • Loading branch information
xjasonlyu committed Mar 29, 2022
1 parent e6911cb commit b166ed5
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 36 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
8 changes: 2 additions & 6 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 10 additions & 11 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package engine

import (
"errors"
"net"

"github.com/xjasonlyu/tun2socks/v2/component/dialer"
"github.com/xjasonlyu/tun2socks/v2/core"
"github.com/xjasonlyu/tun2socks/v2/core/device"
_ "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"
Expand Down Expand Up @@ -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"`
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
28 changes: 28 additions & 0 deletions engine/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package engine
import (
"encoding/base64"
"fmt"
"net"
"net/url"
"strings"

Expand All @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
2 changes: 1 addition & 1 deletion stats/connections.go → restapi/connections.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stats
package restapi

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion stats/debug.go → restapi/debug.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build debug

package stats
package restapi

import (
"net/http"
Expand Down
2 changes: 1 addition & 1 deletion stats/errors.go → restapi/errors.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stats
package restapi

var (
ErrUnauthorized = newError("Unauthorized")
Expand Down
2 changes: 1 addition & 1 deletion stats/netstats.go → restapi/netstats.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package stats
package restapi

// TODO: Network statistic support.
func init() {}
11 changes: 4 additions & 7 deletions stats/server.go → restapi/server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package stats
package restapi

// Ref: github.com/Dreamacro/clash/hub/route

import (
"bytes"
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 0 additions & 4 deletions stats/stats.go

This file was deleted.

0 comments on commit b166ed5

Please sign in to comment.