github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/cmd/socat/socat.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  	"fmt"
     7  	"log"
     8  	"net"
     9  	"os"
    10  
    11  	"github.com/Asutorufa/yuhaiin/pkg/net/netapi"
    12  	"github.com/Asutorufa/yuhaiin/pkg/net/proxy/socks5"
    13  	"github.com/Asutorufa/yuhaiin/pkg/protos/statistic"
    14  	"github.com/Asutorufa/yuhaiin/pkg/utils/relay"
    15  )
    16  
    17  func main() {
    18  	target := flag.String("t", "", "target, -t www.example.com:22")
    19  	s5 := flag.String("s", "127.0.0.1:1080", "socks5 server host, -s 127.0.0.1:1080")
    20  	flag.Parse()
    21  
    22  	addr, err := netapi.ParseAddress(statistic.Type_tcp, *target)
    23  	if err != nil {
    24  		log.Fatal(err)
    25  	}
    26  
    27  	host, port, err := net.SplitHostPort(*s5)
    28  	if err != nil {
    29  		log.Fatal(err)
    30  	}
    31  
    32  	conn, err := socks5.Dial(host, port, "", "").Conn(context.TODO(), addr)
    33  	if err != nil {
    34  		log.Fatal(err)
    35  	}
    36  	defer conn.Close()
    37  
    38  	fmt.Fprintf(os.Stderr, "start relay %v <-> %v\n\n", addr, conn.RemoteAddr())
    39  
    40  	relay.Relay(&stdInOutReadWriteCloser{}, conn)
    41  }
    42  
    43  type stdInOutReadWriteCloser struct{}
    44  
    45  func (stdInOutReadWriteCloser) Read(b []byte) (int, error)  { return os.Stdin.Read(b) }
    46  func (stdInOutReadWriteCloser) Write(b []byte) (int, error) { return os.Stdout.Write(b) }
    47  func (stdInOutReadWriteCloser) Close() error                { os.Stdin.Close(); return os.Stdout.Close() }