mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-10-12 04:01:13 +08:00
Chore: rename secret to token
This commit is contained in:
@@ -26,6 +26,6 @@ ENV EXCLUDED=
|
|||||||
ENV EXTRACMD=
|
ENV EXTRACMD=
|
||||||
ENV PROXY=
|
ENV PROXY=
|
||||||
ENV STATS=
|
ENV STATS=
|
||||||
ENV SECRET=
|
ENV TOKEN=
|
||||||
|
|
||||||
ENTRYPOINT ["/entrypoint.sh"]
|
ENTRYPOINT ["/entrypoint.sh"]
|
||||||
|
@@ -254,8 +254,8 @@ Usage of tun2socks:
|
|||||||
-l, --loglevel string Log level [debug|info|warn|error|silent] (default "info")
|
-l, --loglevel string Log level [debug|info|warn|error|silent] (default "info")
|
||||||
-m, --mtu int Maximum transmission unit
|
-m, --mtu int Maximum transmission unit
|
||||||
-p, --proxy string Use this proxy [protocol://]host[:port]
|
-p, --proxy string Use this proxy [protocol://]host[:port]
|
||||||
--secret string HTTP statistic server auth secret
|
|
||||||
--stats string HTTP statistic server listen address
|
--stats string HTTP statistic server listen address
|
||||||
|
--token string HTTP statistic server auth token
|
||||||
-v, --version Show version information and quit
|
-v, --version Show version information and quit
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@@ -9,12 +9,12 @@ services:
|
|||||||
- '/dev/net/tun:/dev/net/tun'
|
- '/dev/net/tun:/dev/net/tun'
|
||||||
environment:
|
environment:
|
||||||
- GODEBUG=madvdontneed=1
|
- GODEBUG=madvdontneed=1
|
||||||
- PROXY=
|
|
||||||
- LOGLEVEL=
|
- LOGLEVEL=
|
||||||
- STATS=
|
|
||||||
- SECRET=
|
|
||||||
- EXCLUDED=
|
- EXCLUDED=
|
||||||
- EXTRACMD=
|
- EXTRACMD=
|
||||||
|
- PROXY=
|
||||||
|
- STATS=
|
||||||
|
- TOKEN=
|
||||||
networks:
|
networks:
|
||||||
switch:
|
switch:
|
||||||
ipv4_address: 172.20.1.2
|
ipv4_address: 172.20.1.2
|
||||||
|
@@ -63,8 +63,8 @@ main() {
|
|||||||
ARGS="--stats $STATS"
|
ARGS="--stats $STATS"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -n "$SECRET" ]; then
|
if [ -n "$TOKEN" ]; then
|
||||||
ARGS="$ARGS --secret $SECRET"
|
ARGS="$ARGS --token $TOKEN"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
exec tun2socks \
|
exec tun2socks \
|
||||||
|
@@ -12,8 +12,8 @@ import (
|
|||||||
type Engine struct {
|
type Engine struct {
|
||||||
mtu uint32
|
mtu uint32
|
||||||
iface string
|
iface string
|
||||||
secret string
|
|
||||||
stats string
|
stats string
|
||||||
|
token string
|
||||||
logLevel string
|
logLevel string
|
||||||
rawProxy string
|
rawProxy string
|
||||||
rawDevice string
|
rawDevice string
|
||||||
@@ -76,7 +76,7 @@ func (e *Engine) setInterface() error {
|
|||||||
func (e *Engine) setStats() error {
|
func (e *Engine) setStats() error {
|
||||||
if e.stats != "" {
|
if e.stats != "" {
|
||||||
go func() {
|
go func() {
|
||||||
_ = stats.Start(e.stats, e.secret)
|
_ = stats.Start(e.stats, e.token)
|
||||||
}()
|
}()
|
||||||
log.Infof("[STATS] listen and serve at: http://%s", e.stats)
|
log.Infof("[STATS] listen and serve at: http://%s", e.stats)
|
||||||
}
|
}
|
||||||
|
@@ -32,9 +32,9 @@ func WithProxy(proxy string) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithStats(stats, secret string) Option {
|
func WithStats(stats, token string) Option {
|
||||||
return func(e *Engine) {
|
return func(e *Engine) {
|
||||||
e.stats = stats
|
e.stats = stats
|
||||||
e.secret = secret
|
e.token = token
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
6
main.go
6
main.go
@@ -19,8 +19,8 @@ var (
|
|||||||
iface string
|
iface string
|
||||||
level string
|
level string
|
||||||
proxy string
|
proxy string
|
||||||
secret string
|
|
||||||
stats string
|
stats string
|
||||||
|
token string
|
||||||
mtu int
|
mtu int
|
||||||
version bool
|
version bool
|
||||||
)
|
)
|
||||||
@@ -30,8 +30,8 @@ func init() {
|
|||||||
flag.StringVarP(&iface, "interface", "i", "", "Use network INTERFACE (Darwin/Linux only)")
|
flag.StringVarP(&iface, "interface", "i", "", "Use network INTERFACE (Darwin/Linux only)")
|
||||||
flag.StringVarP(&proxy, "proxy", "p", "", "Use this proxy [protocol://]host[:port]")
|
flag.StringVarP(&proxy, "proxy", "p", "", "Use this proxy [protocol://]host[:port]")
|
||||||
flag.StringVarP(&level, "loglevel", "l", "info", "Log level [debug|info|warn|error|silent]")
|
flag.StringVarP(&level, "loglevel", "l", "info", "Log level [debug|info|warn|error|silent]")
|
||||||
flag.StringVar(&secret, "secret", "", "HTTP statistic server auth secret")
|
|
||||||
flag.StringVar(&stats, "stats", "", "HTTP statistic server listen address")
|
flag.StringVar(&stats, "stats", "", "HTTP statistic server listen address")
|
||||||
|
flag.StringVar(&token, "token", "", "HTTP statistic server auth token")
|
||||||
flag.IntVarP(&mtu, "mtu", "m", 0, "Maximum transmission unit")
|
flag.IntVarP(&mtu, "mtu", "m", 0, "Maximum transmission unit")
|
||||||
flag.BoolVarP(&version, "version", "v", false, "Show version information and quit")
|
flag.BoolVarP(&version, "version", "v", false, "Show version information and quit")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
@@ -56,7 +56,7 @@ func main() {
|
|||||||
engine.WithLogLevel(level),
|
engine.WithLogLevel(level),
|
||||||
engine.WithMTU(mtu),
|
engine.WithMTU(mtu),
|
||||||
engine.WithProxy(proxy),
|
engine.WithProxy(proxy),
|
||||||
engine.WithStats(stats, secret),
|
engine.WithStats(stats, token),
|
||||||
}
|
}
|
||||||
|
|
||||||
eng := engine.New(options...)
|
eng := engine.New(options...)
|
||||||
|
@@ -26,7 +26,7 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func Start(addr, secret string) error {
|
func Start(addr, token string) error {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
|
|
||||||
c := cors.New(cors.Options{
|
c := cors.New(cors.Options{
|
||||||
@@ -38,7 +38,7 @@ func Start(addr, secret string) error {
|
|||||||
|
|
||||||
r.Use(c.Handler)
|
r.Use(c.Handler)
|
||||||
r.Group(func(r chi.Router) {
|
r.Group(func(r chi.Router) {
|
||||||
r.Use(authenticator(secret))
|
r.Use(authenticator(token))
|
||||||
r.Get("/", hello)
|
r.Get("/", hello)
|
||||||
r.Get("/logs", getLogs)
|
r.Get("/logs", getLogs)
|
||||||
r.Get("/traffic", traffic)
|
r.Get("/traffic", traffic)
|
||||||
@@ -63,18 +63,18 @@ func hello(w http.ResponseWriter, r *http.Request) {
|
|||||||
render.JSON(w, r, render.M{"hello": constant.Name})
|
render.JSON(w, r, render.M{"hello": constant.Name})
|
||||||
}
|
}
|
||||||
|
|
||||||
func authenticator(secret string) func(http.Handler) http.Handler {
|
func authenticator(token string) func(http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||||
if secret == "" {
|
if token == "" {
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Browser websocket not support custom header
|
// Browser websocket not support custom header
|
||||||
if websocket.IsWebSocketUpgrade(r) && r.URL.Query().Get("token") != "" {
|
if websocket.IsWebSocketUpgrade(r) && r.URL.Query().Get("token") != "" {
|
||||||
token := r.URL.Query().Get("token")
|
t := r.URL.Query().Get("token")
|
||||||
if token != secret {
|
if t != token {
|
||||||
render.Status(r, http.StatusUnauthorized)
|
render.Status(r, http.StatusUnauthorized)
|
||||||
render.JSON(w, r, ErrUnauthorized)
|
render.JSON(w, r, ErrUnauthorized)
|
||||||
return
|
return
|
||||||
@@ -87,8 +87,8 @@ func authenticator(secret string) func(http.Handler) http.Handler {
|
|||||||
text := strings.SplitN(header, " ", 2)
|
text := strings.SplitN(header, " ", 2)
|
||||||
|
|
||||||
hasInvalidHeader := text[0] != "Bearer"
|
hasInvalidHeader := text[0] != "Bearer"
|
||||||
hasInvalidSecret := len(text) != 2 || text[1] != secret
|
hasInvalidToken := len(text) != 2 || text[1] != token
|
||||||
if hasInvalidHeader || hasInvalidSecret {
|
if hasInvalidHeader || hasInvalidToken {
|
||||||
render.Status(r, http.StatusUnauthorized)
|
render.Status(r, http.StatusUnauthorized)
|
||||||
render.JSON(w, r, ErrUnauthorized)
|
render.JSON(w, r, ErrUnauthorized)
|
||||||
return
|
return
|
||||||
|
Reference in New Issue
Block a user