基于go语言的socks5工具


基于

https://github.com/armon/go-socks5

socks5协议原生工具

前期通过socks5协议写了一个工具,结果流量太大就会出现溢出。太垃圾了。还是用别人的轮子好用

正向socks5

用法bindSocks -p [port] -U [username] -P [password]

package main

import (
    "flag"
    "fmt"

    socks5 "github.com/armon/go-socks5"
)

var (
    p string
    U string
    P string
)

func init() {
    flag.StringVar(&p, "p", "9081", "listen port for receiver port")
    flag.StringVar(&U, "U", "", "The username of socks5")
    flag.StringVar(&P, "P", "", "The password of socks5")
}
func main() {
    flag.Usage = func() {
        fmt.Println("bindSocks -p 9081 -U username -P password")
    }
    flag.Parse()
    err := bindSocks5(p, U, P)
    fmt.Println(err)
}

func bindSocks5(port string, user string, passwd string) error {
    cfg := &socks5.Config{}
    if user != "" || passwd != "" {
        cfg.Credentials = socks5.StaticCredentials(map[string]string{user: passwd})
    }
    server, err := socks5.New(cfg)
    if err != nil {
        return err
    }

    if err := server.ListenAndServe("tcp", "0.0.0.0:"+port); err != nil {
        return err
    }
    return nil
}

反向socks5

用法:server -t [client port] -c [you connect port]

server端


package main

import (
    "errors"
    "flag"
    "fmt"
    "io"
    "log"
    "net"
    "github.com/hashicorp/yamux"
)

var (
    cport string
    tport string
)
var session *yamux.Session

func init() {
    flag.StringVar(&tport, "t", "9902", "The port of the target connection,Default 9902")
    flag.StringVar(&cport, "c", "127.0.0.1:9901", "What port do you connect to,Default 127.0.0.1:9901")
}
func main() {
    flag.Usage = func() {
        fmt.Println("server -t 9902 -c 127.0.0.1:9901")
    }
    flag.Parse()
    go targetPort(tport)
    yourAddr(cport)
}
func targetPort(address string) (err error) {
    log.Println("Waiting for target in port: " + address)
    ln, err := net.Listen("tcp", "0.0.0.0:"+address)
    if err != nil {
        return errors.New("start port error: " + err.Error())
    }
    for {
        conn, err := ln.Accept()
        if err != nil {
            return errors.New("Accept port error")
        }
        log.Println("Connected to target: ", conn.RemoteAddr().String())
        session, err = yamux.Client(conn, nil)
    }
}
func yourAddr(address string) error {
    log.Println("Waiting for clients in port: " + address)
    ln, err := net.Listen("tcp", address)
    if err != nil {
        return errors.New("start port error: " + err.Error())
    }
    for {
        conn, err := ln.Accept()
        log.Println("Connected to client: ", conn.RemoteAddr().String())
        if err != nil {
            return errors.New("Accept port error")
        }
        if session == nil {
            log.Println("Error target not connect")
            conn.Close()
            continue
        }
        stream, err := session.Open()
        if err != nil {
            return err
        }
        go func() {
            io.Copy(conn, stream)
            conn.Close()
        }()
        go func() {
            io.Copy(stream, conn)
            stream.Close()
        }()
    }
}

client端

用法:
client -s [server ip]:[client port]
client -s [server ip]:[client port] -u [username] -p [password]

package main

import (
    "flag"
    "fmt"
    "log"
    "net"
    "github.com/hashicorp/yamux"

    "github.com/armon/go-socks5"
)

var session *yamux.Session
var (
    server string
    user   string
    passwd string
)

func init() {
    flag.StringVar(&server, "s", "", "The server ip and port,Example: 192.168.1.1:9902")
    flag.StringVar(&user, "u", "", "username")
    flag.StringVar(&passwd, "p", "", "password")
}
func main() {
    flag.Usage = func() {
        fmt.Println("client -s 192.168.1.1:9902")
        fmt.Println("client -s 192.168.1.1:9902 -u admin -p admin")
    }
    flag.Parse()
    if server == "" {
        fmt.Println("Parameter error")
        return
    }
    connectSocks(server)
}
func connectSocks(address string) error {
    cfg := &socks5.Config{}
    if user != "" || passwd != "" {
        cfg.Credentials = socks5.StaticCredentials(map[string]string{user: passwd})
    }
    server, err := socks5.New(cfg)
    if err != nil {
        return err
    }
    var conn net.Conn
    conn, err = net.Dial("tcp", address)
    if err != nil {
        return err
    }
    log.Println("Connect server success: " + address)
    session, err = yamux.Server(conn, nil)
    if err != nil {
        return err
    }
    for {
        stream, err := session.Accept()
        if err != nil {
            return err
        }
        go func() {
            err = server.ServeConn(stream)
            if err != nil {
                log.Println(err)
            }
        }()
    }
}

编译好的文件

socks5.7z

不足

没实现流量加密和流量压缩

声明:john'blog|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - 基于go语言的socks5工具


欢迎来到脚本小子的博客