mirror of
https://github.com/cedar2025/hysteria.git
synced 2025-06-10 14:39:54 +00:00
feat: client connect & disconnect log for server
This commit is contained in:
parent
80b1ce33a2
commit
013e44a9c5
@ -131,7 +131,7 @@ func initApp(c *cli.Context) error {
|
|||||||
"version", "url",
|
"version", "url",
|
||||||
"config", "file", "mode",
|
"config", "file", "mode",
|
||||||
"addr", "src", "dst", "session", "action",
|
"addr", "src", "dst", "session", "action",
|
||||||
"error",
|
"msg", "error",
|
||||||
},
|
},
|
||||||
TimestampFormat: c.String("log-timestamp"),
|
TimestampFormat: c.String("log-timestamp"),
|
||||||
})
|
})
|
||||||
|
@ -128,6 +128,20 @@ func server(config *serverConfig) {
|
|||||||
default:
|
default:
|
||||||
logrus.WithField("mode", config.Auth.Mode).Fatal("Unsupported authentication mode")
|
logrus.WithField("mode", config.Auth.Mode).Fatal("Unsupported authentication mode")
|
||||||
}
|
}
|
||||||
|
connectFunc := func(addr net.Addr, auth []byte, sSend uint64, sRecv uint64) (bool, string) {
|
||||||
|
ok, msg := authFunc(addr, auth, sSend, sRecv)
|
||||||
|
if !ok {
|
||||||
|
logrus.WithFields(logrus.Fields{
|
||||||
|
"src": addr,
|
||||||
|
"msg": msg,
|
||||||
|
}).Info("Authentication failed, client rejected")
|
||||||
|
} else {
|
||||||
|
logrus.WithFields(logrus.Fields{
|
||||||
|
"src": addr,
|
||||||
|
}).Info("Client connected")
|
||||||
|
}
|
||||||
|
return ok, msg
|
||||||
|
}
|
||||||
// Obfuscator
|
// Obfuscator
|
||||||
var obfuscator obfs.Obfuscator
|
var obfuscator obfs.Obfuscator
|
||||||
if len(config.Obfs) > 0 {
|
if len(config.Obfs) > 0 {
|
||||||
@ -169,7 +183,7 @@ func server(config *serverConfig) {
|
|||||||
uint64(config.UpMbps)*mbpsToBps, uint64(config.DownMbps)*mbpsToBps,
|
uint64(config.UpMbps)*mbpsToBps, uint64(config.DownMbps)*mbpsToBps,
|
||||||
func(refBPS uint64) congestion.CongestionControl {
|
func(refBPS uint64) congestion.CongestionControl {
|
||||||
return hyCongestion.NewBrutalSender(congestion.ByteCount(refBPS))
|
return hyCongestion.NewBrutalSender(congestion.ByteCount(refBPS))
|
||||||
}, config.DisableUDP, aclEngine, obfuscator, authFunc,
|
}, config.DisableUDP, aclEngine, obfuscator, connectFunc, disconnectFunc,
|
||||||
tcpRequestFunc, tcpErrorFunc, udpRequestFunc, udpErrorFunc, promReg)
|
tcpRequestFunc, tcpErrorFunc, udpRequestFunc, udpErrorFunc, promReg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithField("error", err).Fatal("Failed to initialize server")
|
logrus.WithField("error", err).Fatal("Failed to initialize server")
|
||||||
@ -181,6 +195,13 @@ func server(config *serverConfig) {
|
|||||||
logrus.WithField("error", err).Fatal("Server shutdown")
|
logrus.WithField("error", err).Fatal("Server shutdown")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func disconnectFunc(addr net.Addr, auth []byte, err error) {
|
||||||
|
logrus.WithFields(logrus.Fields{
|
||||||
|
"src": addr,
|
||||||
|
"error": err,
|
||||||
|
}).Info("Client disconnected")
|
||||||
|
}
|
||||||
|
|
||||||
func tcpRequestFunc(addr net.Addr, auth []byte, reqAddr string, action acl.Action, arg string) {
|
func tcpRequestFunc(addr net.Addr, auth []byte, reqAddr string, action acl.Action, arg string) {
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"src": addr.String(),
|
"src": addr.String(),
|
||||||
|
@ -14,7 +14,8 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AuthFunc func(addr net.Addr, auth []byte, sSend uint64, sRecv uint64) (bool, string)
|
type ConnectFunc func(addr net.Addr, auth []byte, sSend uint64, sRecv uint64) (bool, string)
|
||||||
|
type DisconnectFunc func(addr net.Addr, auth []byte, err error)
|
||||||
type TCPRequestFunc func(addr net.Addr, auth []byte, reqAddr string, action acl.Action, arg string)
|
type TCPRequestFunc func(addr net.Addr, auth []byte, reqAddr string, action acl.Action, arg string)
|
||||||
type TCPErrorFunc func(addr net.Addr, auth []byte, reqAddr string, err error)
|
type TCPErrorFunc func(addr net.Addr, auth []byte, reqAddr string, err error)
|
||||||
type UDPRequestFunc func(addr net.Addr, auth []byte, sessionID uint32)
|
type UDPRequestFunc func(addr net.Addr, auth []byte, sessionID uint32)
|
||||||
@ -27,7 +28,8 @@ type Server struct {
|
|||||||
disableUDP bool
|
disableUDP bool
|
||||||
aclEngine *acl.Engine
|
aclEngine *acl.Engine
|
||||||
|
|
||||||
authFunc AuthFunc
|
connectFunc ConnectFunc
|
||||||
|
disconnectFunc DisconnectFunc
|
||||||
tcpRequestFunc TCPRequestFunc
|
tcpRequestFunc TCPRequestFunc
|
||||||
tcpErrorFunc TCPErrorFunc
|
tcpErrorFunc TCPErrorFunc
|
||||||
udpRequestFunc UDPRequestFunc
|
udpRequestFunc UDPRequestFunc
|
||||||
@ -41,7 +43,8 @@ type Server struct {
|
|||||||
|
|
||||||
func NewServer(addr string, protocol string, tlsConfig *tls.Config, quicConfig *quic.Config, transport transport2.Transport,
|
func NewServer(addr string, protocol string, tlsConfig *tls.Config, quicConfig *quic.Config, transport transport2.Transport,
|
||||||
sendBPS uint64, recvBPS uint64, congestionFactory CongestionFactory, disableUDP bool, aclEngine *acl.Engine,
|
sendBPS uint64, recvBPS uint64, congestionFactory CongestionFactory, disableUDP bool, aclEngine *acl.Engine,
|
||||||
obfuscator obfs.Obfuscator, authFunc AuthFunc, tcpRequestFunc TCPRequestFunc, tcpErrorFunc TCPErrorFunc,
|
obfuscator obfs.Obfuscator, connectFunc ConnectFunc, disconnectFunc DisconnectFunc,
|
||||||
|
tcpRequestFunc TCPRequestFunc, tcpErrorFunc TCPErrorFunc,
|
||||||
udpRequestFunc UDPRequestFunc, udpErrorFunc UDPErrorFunc, promRegistry *prometheus.Registry) (*Server, error) {
|
udpRequestFunc UDPRequestFunc, udpErrorFunc UDPErrorFunc, promRegistry *prometheus.Registry) (*Server, error) {
|
||||||
pktConn, err := transport.QUICPacketConn(protocol, true, addr, "", obfuscator)
|
pktConn, err := transport.QUICPacketConn(protocol, true, addr, "", obfuscator)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -60,7 +63,8 @@ func NewServer(addr string, protocol string, tlsConfig *tls.Config, quicConfig *
|
|||||||
congestionFactory: congestionFactory,
|
congestionFactory: congestionFactory,
|
||||||
disableUDP: disableUDP,
|
disableUDP: disableUDP,
|
||||||
aclEngine: aclEngine,
|
aclEngine: aclEngine,
|
||||||
authFunc: authFunc,
|
connectFunc: connectFunc,
|
||||||
|
disconnectFunc: disconnectFunc,
|
||||||
tcpRequestFunc: tcpRequestFunc,
|
tcpRequestFunc: tcpRequestFunc,
|
||||||
tcpErrorFunc: tcpErrorFunc,
|
tcpErrorFunc: tcpErrorFunc,
|
||||||
udpRequestFunc: udpRequestFunc,
|
udpRequestFunc: udpRequestFunc,
|
||||||
@ -118,8 +122,9 @@ func (s *Server) handleClient(cs quic.Session) {
|
|||||||
sc := newServerClient(cs, s.transport, auth, s.disableUDP, s.aclEngine,
|
sc := newServerClient(cs, s.transport, auth, s.disableUDP, s.aclEngine,
|
||||||
s.tcpRequestFunc, s.tcpErrorFunc, s.udpRequestFunc, s.udpErrorFunc,
|
s.tcpRequestFunc, s.tcpErrorFunc, s.udpRequestFunc, s.udpErrorFunc,
|
||||||
s.upCounterVec, s.downCounterVec, s.connGaugeVec)
|
s.upCounterVec, s.downCounterVec, s.connGaugeVec)
|
||||||
sc.Run()
|
err = sc.Run()
|
||||||
_ = cs.CloseWithError(closeErrorCodeGeneric, "")
|
_ = cs.CloseWithError(closeErrorCodeGeneric, "")
|
||||||
|
s.disconnectFunc(cs.RemoteAddr(), auth, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auth & negotiate speed
|
// Auth & negotiate speed
|
||||||
@ -151,7 +156,7 @@ func (s *Server) handleControlStream(cs quic.Session, stream quic.Stream) ([]byt
|
|||||||
serverRecvBPS = s.recvBPS
|
serverRecvBPS = s.recvBPS
|
||||||
}
|
}
|
||||||
// Auth
|
// Auth
|
||||||
ok, msg := s.authFunc(cs.RemoteAddr(), ch.Auth, serverSendBPS, serverRecvBPS)
|
ok, msg := s.connectFunc(cs.RemoteAddr(), ch.Auth, serverSendBPS, serverRecvBPS)
|
||||||
// Response
|
// Response
|
||||||
err = struc.Pack(stream, &serverHello{
|
err = struc.Pack(stream, &serverHello{
|
||||||
OK: ok,
|
OK: ok,
|
||||||
|
@ -64,7 +64,7 @@ func newServerClient(cs quic.Session, transport transport.Transport, auth []byte
|
|||||||
return sc
|
return sc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *serverClient) Run() {
|
func (c *serverClient) Run() error {
|
||||||
if !c.DisableUDP {
|
if !c.DisableUDP {
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
@ -79,7 +79,7 @@ func (c *serverClient) Run() {
|
|||||||
for {
|
for {
|
||||||
stream, err := c.CS.AcceptStream(context.Background())
|
stream, err := c.CS.AcceptStream(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
return err
|
||||||
}
|
}
|
||||||
if c.ConnGauge != nil {
|
if c.ConnGauge != nil {
|
||||||
c.ConnGauge.Inc()
|
c.ConnGauge.Inc()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user