diff --git a/internal/nodes/http_request_reverse_proxy.go b/internal/nodes/http_request_reverse_proxy.go index d467694..f050872 100644 --- a/internal/nodes/http_request_reverse_proxy.go +++ b/internal/nodes/http_request_reverse_proxy.go @@ -40,7 +40,7 @@ func (this *HTTPRequest) doReverseProxy() { requestCall.CallResponseCallbacks(this.writer) if origin == nil { err := errors.New(this.requestFullURL() + ": no available origin sites for reverse proxy") - remotelogs.ServerError(this.Server.Id, "HTTP_REQUEST_REVERSE_PROXY", err.Error()) + remotelogs.ServerError(this.Server.Id, "HTTP_REQUEST_REVERSE_PROXY", err.Error(), "", nil) this.write50x(err, http.StatusBadGateway) return } diff --git a/internal/nodes/listener_base.go b/internal/nodes/listener_base.go index 2336154..2a89ff0 100644 --- a/internal/nodes/listener_base.go +++ b/internal/nodes/listener_base.go @@ -137,7 +137,7 @@ func (this *BaseListener) matchSSL(domain string) (*sslconfigs.SSLPolicy, *tls.C } if len(sslConfig.Certs) == 0 { - remotelogs.ServerError(server.Id, "BASE_LISTENER", "no ssl certs found for '"+domain+"', server id: "+types.String(server.Id)) + remotelogs.ServerError(server.Id, "BASE_LISTENER", "no ssl certs found for '"+domain+"', server id: "+types.String(server.Id), "", nil) } return sslConfig, sslConfig.FirstCert(), nil diff --git a/internal/nodes/listener_manager.go b/internal/nodes/listener_manager.go index 319581d..656c7f0 100644 --- a/internal/nodes/listener_manager.go +++ b/internal/nodes/listener_manager.go @@ -8,6 +8,7 @@ import ( "github.com/TeaOSLab/EdgeNode/internal/remotelogs" "github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/lists" + "github.com/iwind/TeaGo/maps" "net/url" "os/exec" "regexp" @@ -123,20 +124,19 @@ func (this *ListenerManager) Start(node *nodeconfigs.NodeConfig) error { portIndex := strings.LastIndex(addr, ":") if portIndex > 0 { var port = addr[portIndex+1:] - var processName = this.findProcessNameWithPort(port) + var processName = this.findProcessNameWithPort(group.IsUDP(), port) if len(processName) > 0 { err = errors.New(err.Error() + " (the process using port: '" + processName + "')") } } } - remotelogs.ServerError(firstServer.Id, "LISTENER_MANAGER", err.Error()) + remotelogs.ServerError(firstServer.Id, "LISTENER_MANAGER", "listen '"+addr+"' failed: "+err.Error(), nodeconfigs.NodeLogTypeListenAddressFailed, maps.Map{"address": addr}) } continue } else { // TODO 是否是从错误中恢复 - } this.listenersMap[addr] = listener } @@ -179,14 +179,12 @@ func (this *ListenerManager) retryListeners() { if err == nil { delete(this.retryListenerMap, addr) this.listenersMap[addr] = listener - remotelogs.ServerSuccess(listener.group.FirstServer().Id, "LISTENER_MANAGER", "retry to listen '"+addr+"' successfully") - - // TODO 删除失败记录 + remotelogs.ServerSuccess(listener.group.FirstServer().Id, "LISTENER_MANAGER", "retry to listen '"+addr+"' successfully", nodeconfigs.NodeLogTypeListenAddressFailed, maps.Map{"address": addr}) } } } -func (this *ListenerManager) findProcessNameWithPort(port string) string { +func (this *ListenerManager) findProcessNameWithPort(isUdp bool, port string) string { if runtime.GOOS != "linux" { return "" } @@ -196,7 +194,12 @@ func (this *ListenerManager) findProcessNameWithPort(port string) string { return "" } - var cmd = exec.Command(path, "-tlpn", "sport = :"+port) + var option = "t" + if isUdp { + option = "u" + } + + var cmd = exec.Command(path, "-"+option+"lpn", "sport = :"+port) var output = &bytes.Buffer{} cmd.Stdout = output err = cmd.Run() diff --git a/internal/nodes/listener_tcp.go b/internal/nodes/listener_tcp.go index 32f6538..6bcc311 100644 --- a/internal/nodes/listener_tcp.go +++ b/internal/nodes/listener_tcp.go @@ -177,7 +177,7 @@ func (this *TCPListener) connectOrigin(serverId int64, reverseProxy *serverconfi } conn, err = OriginConnect(origin, remoteAddr) if err != nil { - remotelogs.ServerError(serverId, "TCP_LISTENER", "unable to connect origin: "+origin.Addr.Host+":"+origin.Addr.PortRange+": "+err.Error()) + remotelogs.ServerError(serverId, "TCP_LISTENER", "unable to connect origin: "+origin.Addr.Host+":"+origin.Addr.PortRange+": "+err.Error(), "", nil) continue } else { return diff --git a/internal/nodes/listener_udp.go b/internal/nodes/listener_udp.go index 2292277..fa24457 100644 --- a/internal/nodes/listener_udp.go +++ b/internal/nodes/listener_udp.go @@ -114,7 +114,7 @@ func (this *UDPListener) connectOrigin(serverId int64, reverseProxy *serverconfi } conn, err = OriginConnect(origin, remoteAddr.String()) if err != nil { - remotelogs.ServerError(serverId, "UDP_LISTENER", "unable to connect origin: "+origin.Addr.Host+":"+origin.Addr.PortRange+": "+err.Error()) + remotelogs.ServerError(serverId, "UDP_LISTENER", "unable to connect origin: "+origin.Addr.Host+":"+origin.Addr.PortRange+": "+err.Error(), "", nil) continue } else { // PROXY Protocol diff --git a/internal/remotelogs/utils.go b/internal/remotelogs/utils.go index f7dfdf8..7ab6550 100644 --- a/internal/remotelogs/utils.go +++ b/internal/remotelogs/utils.go @@ -1,6 +1,7 @@ package remotelogs import ( + "encoding/json" "github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" teaconst "github.com/TeaOSLab/EdgeNode/internal/const" @@ -8,6 +9,7 @@ import ( "github.com/TeaOSLab/EdgeNode/internal/trackers" "github.com/cespare/xxhash" "github.com/iwind/TeaGo/logs" + "github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/types" "time" ) @@ -33,18 +35,13 @@ func init() { func Println(tag string, description string) { logs.Println("[" + tag + "]" + description) - nodeConfig, _ := nodeconfigs.SharedNodeConfig() - if nodeConfig == nil { - return - } - select { case logChan <- &pb.NodeLog{ Role: teaconst.Role, Tag: tag, Description: description, Level: "info", - NodeId: nodeConfig.Id, + NodeId: teaconst.NodeId, CreatedAt: time.Now().Unix(), }: default: @@ -56,18 +53,13 @@ func Println(tag string, description string) { func Warn(tag string, description string) { logs.Println("[" + tag + "]" + description) - nodeConfig, _ := nodeconfigs.SharedNodeConfig() - if nodeConfig == nil { - return - } - select { case logChan <- &pb.NodeLog{ Role: teaconst.Role, Tag: tag, Description: description, Level: "warning", - NodeId: nodeConfig.Id, + NodeId: teaconst.NodeId, CreatedAt: time.Now().Unix(), }: default: @@ -79,18 +71,13 @@ func Warn(tag string, description string) { func Error(tag string, description string) { logs.Println("[" + tag + "]" + description) - nodeConfig, _ := nodeconfigs.SharedNodeConfig() - if nodeConfig == nil { - return - } - select { case logChan <- &pb.NodeLog{ Role: teaconst.Role, Tag: tag, Description: description, Level: "error", - NodeId: nodeConfig.Id, + NodeId: teaconst.NodeId, CreatedAt: time.Now().Unix(), }: default: @@ -111,12 +98,18 @@ func ErrorObject(tag string, err error) { } // ServerError 打印服务相关错误信息 -func ServerError(serverId int64, tag string, description string) { +func ServerError(serverId int64, tag string, description string, logType nodeconfigs.NodeLogType, params maps.Map) { logs.Println("[" + tag + "]" + description) - nodeConfig, _ := nodeconfigs.SharedNodeConfig() - if nodeConfig == nil { - return + // 参数 + var paramsJSON []byte + if len(params) > 0 { + p, err := json.Marshal(params) + if err != nil { + logs.Println("[LOG]" + err.Error()) + } else { + paramsJSON = p + } } select { @@ -125,9 +118,11 @@ func ServerError(serverId int64, tag string, description string) { Tag: tag, Description: description, Level: "error", - NodeId: nodeConfig.Id, + NodeId: teaconst.NodeId, ServerId: serverId, CreatedAt: time.Now().Unix(), + Type: logType, + ParamsJSON: paramsJSON, }: default: @@ -135,12 +130,18 @@ func ServerError(serverId int64, tag string, description string) { } // ServerSuccess 打印服务相关成功信息 -func ServerSuccess(serverId int64, tag string, description string) { +func ServerSuccess(serverId int64, tag string, description string, logType nodeconfigs.NodeLogType, params maps.Map) { logs.Println("[" + tag + "]" + description) - nodeConfig, _ := nodeconfigs.SharedNodeConfig() - if nodeConfig == nil { - return + // 参数 + var paramsJSON []byte + if len(params) > 0 { + p, err := json.Marshal(params) + if err != nil { + logs.Println("[LOG]" + err.Error()) + } else { + paramsJSON = p + } } select { @@ -149,9 +150,11 @@ func ServerSuccess(serverId int64, tag string, description string) { Tag: tag, Description: description, Level: "success", - NodeId: nodeConfig.Id, + NodeId: teaconst.NodeId, ServerId: serverId, CreatedAt: time.Now().Unix(), + Type: logType, + ParamsJSON: paramsJSON, }: default: