Skip to content

Commit

Permalink
日志也都打印到日志文件上
Browse files Browse the repository at this point in the history
  • Loading branch information
xtccc committed Jan 30, 2025
1 parent 6b2c0e6 commit cc3b7f7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
5 changes: 2 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"os"

"github.com/sirupsen/logrus"
Expand All @@ -23,13 +22,13 @@ var DomainForwardMap []struct {
func LoadConfig() Config {
data, err := os.ReadFile("config.yaml")
if err != nil {
fmt.Printf("failed to read config file: %v", err)
logrus.Errorf("failed to read config file: %v", err)
return Config{}
}

var cfg Config
if err := yaml.Unmarshal(data, &cfg); err != nil {
fmt.Printf("failed to parse config: %v", err)
logrus.Errorf("failed to parse config: %v", err)
return Config{}
}
n := len(cfg.Rules)
Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func handleConnectRequest(conn net.Conn) {
// 格式为 CONNECT www.google.com:443 HTTP/1.1
parts := strings.Split(reqLine, " ")
if len(parts) < 3 {
fmt.Println("Invalid CONNECT request format")
logrus.Errorln("Invalid CONNECT request format")
return
}

Expand Down Expand Up @@ -141,19 +141,19 @@ func main() {
// 启动代理服务,监听指定地址
listener, err := net.Listen("tcp", *listenAddr)
if err != nil {
fmt.Println("Error starting server:", err)
logrus.Errorln("Error starting server:", err)
return
}

defer listener.Close()

fmt.Printf("Proxy server is running on %s\n", *listenAddr)
logrus.Errorf("Proxy server is running on %s\n", *listenAddr)

// 接受连接
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Error accepting connection:", err)
logrus.Errorln("Error accepting connection:", err)
continue
}

Expand Down
16 changes: 9 additions & 7 deletions tcp_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strconv"
"strings"
"sync"

"github.com/sirupsen/logrus"
)

func readRequestHeaderAndBody(conn net.Conn) (string, []byte, error) {
Expand Down Expand Up @@ -48,7 +50,7 @@ func readRequestHeaderAndBody(conn net.Conn) (string, []byte, error) {
func handleConnectRequest_https(conn net.Conn, target, reqLine string) {
hostPort := strings.Split(target, ":")
if len(hostPort) != 2 {
fmt.Println("Invalid target format")
logrus.Errorln("Invalid target format")
return
}

Expand All @@ -68,42 +70,42 @@ func forward(upstreamHost, forward_method, reqLine string, conn net.Conn) {
// 尝试连接到目标服务器
upstreamConn, err := net.Dial("tcp", upstreamHost)
if err != nil {
fmt.Println("Error connecting to target:", err)
logrus.Errorln("Error connecting to target:", err)
return
}
defer upstreamConn.Close()

// 将客户端的 CONNECT 请求转发给上游代理
_, err = upstreamConn.Write([]byte(reqLine))
if err != nil {
fmt.Println("Error forwarding CONNECT to upstream:", err)
logrus.Errorln("Error forwarding CONNECT to upstream:", err)
return
}

// 读取上游代理的响应
upstream_resp, err := readRequestHeader(upstreamConn)
if err != nil {
fmt.Println("readRequestHeader(upstreamConn) error ", err)
logrus.Errorln("readRequestHeader(upstreamConn) error ", err)
return
}

// 转发上游代理的响应给客户端
_, err = conn.Write([]byte(upstream_resp))
if err != nil {
fmt.Println("Error forwarding response to client:", err)
logrus.Errorln("Error forwarding response to client:", err)
return
}
forward_io_copy(conn, upstreamConn)
} else if forward_method == "direct" {

targetConn, err := net.Dial("tcp", upstreamHost)
if err != nil {
fmt.Println("Error connecting to target:", err)
logrus.Errorln("Error connecting to target:", err)
return
}
_, err = conn.Write([]byte("HTTP/1.1 200 Connection Established\r\n\r\n"))
if err != nil {
fmt.Println("Error writing to client:", err)
logrus.Errorln("Error writing to client:", err)
return
}

Expand Down

0 comments on commit cc3b7f7

Please sign in to comment.