Skip to content

Commit

Permalink
add listen logs
Browse files Browse the repository at this point in the history
Signed-off-by: He Xian <[email protected]>
  • Loading branch information
hexian000 committed Jan 24, 2024
1 parent 3c1c61a commit fee8207
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion v2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type Config struct {
MaxSessions int `json:"maxsessions"`
// (optional) mux accept backlog, default to 256, you may not want to change this
AcceptBacklog int `json:"backlog"`
// (optional) stream window size in bytes, default to 256KiB, increase this on long fat networks
// (optional) stream window size in bytes, default to 256 KiB, increase this on long fat networks
StreamWindow uint32 `json:"window"`
// (optional) tunnel connecting timeout in seconds, default to 15
ConnectTimeout int `json:"timeout"`
Expand Down
2 changes: 1 addition & 1 deletion v2/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (h *TLSHandler) Serve(ctx context.Context, conn net.Conn) {
return
}
slog.Infof("%q <= %v: setup %v", t.name, conn.RemoteAddr(), formats.Duration(time.Since(start)))
h.s.events.Add(time.Now(), fmt.Sprintf("%q <= %v: established", t.name, mux.RemoteAddr()))
h.s.recentEvents.Add(time.Now(), fmt.Sprintf("%q <= %v: established", t.name, mux.RemoteAddr()))
}

// ForwardHandler forwards connections to another plain address
Expand Down
2 changes: 1 addition & 1 deletion v2/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (h *apiStatsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

fprintf(w, "\n> Recent Events\n")
h.s.events.Format(w)
h.s.recentEvents.Format(w)
}

func RunHTTPServer(l net.Listener, s *Server) error {
Expand Down
15 changes: 8 additions & 7 deletions v2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ type Server struct {

f forwarder.Forwarder

flowStats *snet.FlowStats
events eventlog.Recent
flowStats *snet.FlowStats
recentEvents eventlog.Recent

listeners map[string]net.Listener
tunnels map[string]*Tunnel // map[identity]tunnel
Expand Down Expand Up @@ -68,11 +68,11 @@ func NewServer(cfg *Config) *Server {
timeout: cfg.Timeout,
contexts: make(map[context.Context]context.CancelFunc),
},
f: forwarder.New(cfg.MaxConn, g),
flowStats: &snet.FlowStats{},
events: eventlog.NewRecent(16),
g: g,
c: cfg,
f: forwarder.New(cfg.MaxConn, g),
flowStats: &snet.FlowStats{},
recentEvents: eventlog.NewRecent(16),
g: g,
c: cfg,
}
}

Expand Down Expand Up @@ -187,6 +187,7 @@ func (s *Server) Start() error {
if err != nil {
return err
}
slog.Noticef("http listen: %v", l.Addr())
if err := s.g.Go(func() {
if err := RunHTTPServer(l, s); err != nil && !errors.Is(err, net.ErrClosed) {
slog.Error(formats.Error(err))
Expand Down
8 changes: 5 additions & 3 deletions v2/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (t *Tunnel) Start() error {
if err != nil {
return err
}
slog.Noticef("mux listen: %v", l.Addr())
h := &TLSHandler{s: t.s, t: t}
c := t.s.getConfig()
t.l = hlistener.Wrap(l, &hlistener.Config{
Expand All @@ -67,6 +68,7 @@ func (t *Tunnel) Start() error {
if err != nil {
return err
}
slog.Noticef("forward listen: %v", l.Addr())
h := &TunnelHandler{s: t.s, t: t}
if err := t.s.g.Go(func() {
t.s.Serve(l, h)
Expand Down Expand Up @@ -119,7 +121,7 @@ func (t *Tunnel) runWithRedial() {
select {
case mux := <-t.muxCloseSig:
slog.Infof("tunnel %q: connection lost %v", t.name, mux.RemoteAddr())
t.s.events.Add(time.Now(), fmt.Sprintf("%q => %v: connection lost", t.name, mux.RemoteAddr()))
t.s.recentEvents.Add(time.Now(), fmt.Sprintf("%q => %v: connection lost", t.name, mux.RemoteAddr()))
case <-t.scheduleRedial():
case <-t.s.g.CloseC():
// server shutdown
Expand All @@ -145,7 +147,7 @@ func (t *Tunnel) run() {
select {
case mux := <-t.muxCloseSig:
slog.Infof("tunnel %q: connection lost %v", t.name, mux.RemoteAddr())
t.s.events.Add(time.Now(), fmt.Sprintf("%q => %v: connection lost", t.name, mux.RemoteAddr()))
t.s.recentEvents.Add(time.Now(), fmt.Sprintf("%q => %v: connection lost", t.name, mux.RemoteAddr()))
case <-t.s.g.CloseC():
// server shutdown
return
Expand Down Expand Up @@ -271,7 +273,7 @@ func (t *Tunnel) dial(ctx context.Context) (*yamux.Session, error) {
return nil, err
}
slog.Infof("%q => %v: setup %v", t.name, conn.RemoteAddr(), formats.Duration(time.Since(start)))
t.s.events.Add(time.Now(), fmt.Sprintf("%q => %v: established", t.name, mux.RemoteAddr()))
t.s.recentEvents.Add(time.Now(), fmt.Sprintf("%q => %v: established", t.name, mux.RemoteAddr()))
return mux, nil
}

Expand Down

0 comments on commit fee8207

Please sign in to comment.