1.实现中转服务器

使用 linuxiptables进行中转服务

2.简答go代码实现

package main

import (
	"fmt"
	"io"
	"net"
	"sync"
)

func forwardTraffic(srcAddr string, dstAddr string) {
	srcListener, err := net.Listen("tcp", srcAddr)
	if err != nil {
		fmt.Println("Error listening:", err)
		return
	}
	defer srcListener.Close()

	fmt.Printf("Forwarding traffic from %s to %s\n", srcAddr, dstAddr)

	for {
		srcConn, err := srcListener.Accept()
		if err != nil {
			fmt.Println("Error accepting connection:", err)
			continue
		}

		go handleConnection(srcConn, dstAddr)
	}
}

func handleConnection(srcConn net.Conn, dstAddr string) {
	defer srcConn.Close()

	dstConn, err := net.Dial("tcp", dstAddr)
	if err != nil {
		fmt.Println("Error connecting to destination:", err)
		return
	}
	defer dstConn.Close()

	var wg sync.WaitGroup
	wg.Add(2)

	go func() {
		defer wg.Done()
		_, err := io.Copy(dstConn, srcConn)
		if err != nil {
			fmt.Println("Error copying data from source to destination:", err)
		}
	}()

	go func() {
		defer wg.Done()
		_, err := io.Copy(srcConn, dstConn)
		if err != nil {
			fmt.Println("Error copying data from destination to source:", err)
		}
	}()

	wg.Wait()
}

// 实现中转服务器
func main() {
	srcAddr := "127.0.0.1:8080"     // 源端口
	dstAddr := "127.0.0.1:7676" // 目标端口

	forwardTraffic(srcAddr, dstAddr)
}

3.简答go代码实现2(文件配置多个地址)

首先,创建一个名为config.json的配置文件,内容如下:

[
    {
        "src": "localhost:8080",
        "dst": "localhost:9090"
    },
    {
        "src": "localhost:8888",
        "dst": "localhost:9999"
    }
]

接下来,使用以下代码来读取配置文件并启动多个端口转发:

package main

import (
    "encoding/json"
    "fmt"
    "io"
    "net"
    "os"
    "sync"
)

// PortMapping 定义了端口映射的结构体
type PortMapping struct {
    Src string `json:"src"`
    Dst string `json:"dst"`
}

func main() {
    configFile := "config.json" // 配置文件名

    // 读取配置文件
    mappings, err := loadPortMappings(configFile)
    if err != nil {
        fmt.Println("Error loading port mappings:", err)
        return
    }

    // 启动多个端口转发服务器
    var wg sync.WaitGroup
    for _, mapping := range mappings {
        wg.Add(1)
        go func(mapping PortMapping) {
            defer wg.Done()
            forwardTraffic(mapping.Src, mapping.Dst)
        }(mapping)
    }

    fmt.Println("Port forwarding servers are running...")
    wg.Wait()
}

func loadPortMappings(configFile string) ([]PortMapping, error) {
    file, err := os.Open(configFile)
    if err != nil {
        return nil, err
    }
    defer file.Close()

    var mappings []PortMapping
    decoder := json.NewDecoder(file)
    if err := decoder.Decode(&mappings); err != nil {
        return nil, err
    }

    return mappings, nil
}

func forwardTraffic(srcAddr string, dstAddr string) {
    srcListener, err := net.Listen("tcp", srcAddr)
    if err != nil {
        fmt.Println("Error listening:", err)
        return
    }
    defer srcListener.Close()

    fmt.Printf("Forwarding traffic from %s to %s\n", srcAddr, dstAddr)

    for {
        srcConn, err := srcListener.Accept()
        if err != nil {
            fmt.Println("Error accepting connection:", err)
            continue
        }

        go handleConnection(srcConn, dstAddr)
    }
}

func handleConnection(srcConn net.Conn, dstAddr string) {
    defer srcConn.Close()

    dstConn, err := net.Dial("tcp", dstAddr)
    if err != nil {
        fmt.Println("Error connecting to destination:", err)
        return
    }
    defer dstConn.Close()

    var wg sync.WaitGroup
    wg.Add(2)

    go func() {
        defer wg.Done()
        _, err := io.Copy(dstConn, srcConn)
        if err != nil {
            fmt.Println("Error copying data from source to destination:", err)
        }
    }()

    go func() {
        defer wg.Done()
        _, err := io.Copy(srcConn, dstConn)
        if err != nil {
            fmt.Println("Error copying data from destination to source:", err)
        }
    }()

    wg.Wait()
}

4.使用GOST第三方开源服务

直达链接