Skip to content

Commit

Permalink
*: fix web pkg logger and add fallback server for XrayServer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Dec 12, 2023
1 parent 8677101 commit 27dbb9d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
2 changes: 2 additions & 0 deletions examples/xray_trojan.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"reload_interval": 1,
"xray_config": {
"stats": {},
"api": {
Expand Down Expand Up @@ -27,6 +28,7 @@
},
"inbounds": [
{
"listen": "127.0.0.1",
"port": 4443,
"protocol": "trojan",
"tag": "trojan_proxy",
Expand Down
15 changes: 8 additions & 7 deletions internal/web/ping_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/Ehco1996/ehco/internal/config"
"github.com/go-ping/ping"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
)

var (
Expand Down Expand Up @@ -43,10 +44,10 @@ type PingGroup struct {
func initPinger(host string) *ping.Pinger {
pinger := ping.New(host)
if err := pinger.Resolve(); err != nil {
l.Errorf("failed to resolve pinger host:%s err:%s\n", host, err.Error())
zap.S().Named("web").Errorf("failed to resolve pinger host:%s err:%s\n", host, err.Error())
return nil
}
l.Infof("Resolved %s as %s", host, pinger.IPAddr())
zap.S().Named("web").Infof("Resolved %s as %s", host, pinger.IPAddr())
pinger.Interval = pingInterval
pinger.Timeout = time.Duration(math.MaxInt64)
pinger.RecordRtts = false
Expand Down Expand Up @@ -84,11 +85,11 @@ func NewPingGroup(cfg *config.Config) *PingGroup {
pinger.OnRecv = func(pkt *ping.Packet) {
PingResponseDurationSeconds.WithLabelValues(
pkt.IPAddr.String(), pkt.Addr, labelMap[pkt.Addr]).Observe(pkt.Rtt.Seconds())
l.Infof("%d bytes from %s: icmp_seq=%d time=%v ttl=%v",
zap.S().Named("web").Infof("%d bytes from %s: icmp_seq=%d time=%v ttl=%v",
pkt.Nbytes, pkt.Addr, pkt.Seq, pkt.Rtt, pkt.Ttl)
}
pinger.OnDuplicateRecv = func(pkt *ping.Packet) {
l.Infof("%d bytes from %s: icmp_seq=%d time=%v ttl=%v (DUP!)",
zap.S().Named("web").Infof("%d bytes from %s: icmp_seq=%d time=%v ttl=%v (DUP!)",
pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt, pkt.Ttl)
}
pingers[i] = pinger
Expand Down Expand Up @@ -123,14 +124,14 @@ func (pg *PingGroup) Run() {
return
}
splay := time.Duration(pingInterval.Nanoseconds() / int64(len(pg.Pingers)))
l.Infof("Waiting %s between starting pingers", splay)
zap.S().Named("web").Infof("Waiting %s between starting pingers", splay)
for idx := range pg.Pingers {
go func() {
pinger := pg.Pingers[idx]
if err := pinger.Run(); err != nil {
l.Infof("Starting prober err: %s", err)
zap.S().Named("web").Infof("Starting prober err: %s", err)
}
l.Infof("Starting prober for %s", pinger.Addr())
zap.S().Named("web").Infof("Starting prober for %s", pinger.Addr())
}()
time.Sleep(splay)
}
Expand Down
12 changes: 3 additions & 9 deletions internal/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ import (
"go.uber.org/zap"
)

var l *zap.SugaredLogger

func MakeIndexF() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l.Infof("index call from %s", r.RemoteAddr)
zap.S().Named("web").Infof("index call from %s", r.RemoteAddr)
fmt.Fprintf(w, "access from %s \n", r.RemoteAddr)
}
}
Expand Down Expand Up @@ -89,7 +87,7 @@ func simpleTokenAuthMiddleware(token string, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if t := r.URL.Query().Get("token"); t != token {
msg := fmt.Sprintf("un auth request from %s", r.RemoteAddr)
l.Error(msg)
zap.S().Named("web").Error(msg)
hj, ok := w.(http.Hijacker)
if ok {
conn, _, _ := hj.Hijack()
Expand All @@ -104,12 +102,8 @@ func simpleTokenAuthMiddleware(token string, h http.Handler) http.Handler {
}

func StartWebServer(cfg *config.Config) error {
// todo make this only doing once
l = zap.S().Named("web")
// end todo

addr := fmt.Sprintf("0.0.0.0:%d", cfg.WebPort)
l.Infof("Start Web Server at http://%s/", addr)
zap.S().Named("web").Infof("Start Web Server at http://%s/", addr)

r := mux.NewRouter()
AttachProfiler(r)
Expand Down
8 changes: 8 additions & 0 deletions pkg/xray/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ func (xs *XrayServer) Start(ctx context.Context) error {
if err := xs.instance.Start(); err != nil {
return err
}
if xs.fallBack != nil {
go func() {
if err := xs.fallBack.ListenAndServe(); err != nil {
xs.l.Error("fallback server meet error", zap.Error(err))
}
}()
}

if xs.up != nil {
if err := xs.up.Start(ctx); err != nil {
return err
Expand Down

0 comments on commit 27dbb9d

Please sign in to comment.