GithubHelp home page GithubHelp logo

txthinking / socks5 Goto Github PK

View Code? Open in Web Editor NEW
646.0 23.0 114.0 110 KB

SOCKS Protocol Version 5 Library in Go. Full TCP/UDP and IPv4/IPv6 support

Home Page: https://www.txthinking.com

License: MIT License

Go 100.00%
socks socks5 socks-protocol proxy

socks5's Introduction

socks5

中文

Go Report Card GoDoc

🗣 News 🩸 Youtube

SOCKS Protocol Version 5 Library.

Full TCP/UDP and IPv4/IPv6 support. Goals: KISS, less is more, small API, code is like the original protocol.

❤️ A project by txthinking.com

Install

$ go get github.com/txthinking/socks5

Struct is like concept in protocol

  • Negotiation:
    • type NegotiationRequest struct
      • func NewNegotiationRequest(methods []byte), in client
      • func (r *NegotiationRequest) WriteTo(w io.Writer), client writes to server
      • func NewNegotiationRequestFrom(r io.Reader), server reads from client
    • type NegotiationReply struct
      • func NewNegotiationReply(method byte), in server
      • func (r *NegotiationReply) WriteTo(w io.Writer), server writes to client
      • func NewNegotiationReplyFrom(r io.Reader), client reads from server
  • User and password negotiation:
    • type UserPassNegotiationRequest struct
      • func NewUserPassNegotiationRequest(username []byte, password []byte), in client
      • func (r *UserPassNegotiationRequest) WriteTo(w io.Writer), client writes to server
      • func NewUserPassNegotiationRequestFrom(r io.Reader), server reads from client
    • type UserPassNegotiationReply struct
      • func NewUserPassNegotiationReply(status byte), in server
      • func (r *UserPassNegotiationReply) WriteTo(w io.Writer), server writes to client
      • func NewUserPassNegotiationReplyFrom(r io.Reader), client reads from server
  • Request:
    • type Request struct
      • func NewRequest(cmd byte, atyp byte, dstaddr []byte, dstport []byte), in client
      • func (r *Request) WriteTo(w io.Writer), client writes to server
      • func NewRequestFrom(r io.Reader), server reads from client
      • After server gets the client's *Request, processes...
  • Reply:
    • type Reply struct
      • func NewReply(rep byte, atyp byte, bndaddr []byte, bndport []byte), in server
      • func (r *Reply) WriteTo(w io.Writer), server writes to client
      • func NewReplyFrom(r io.Reader), client reads from server
  • Datagram:
    • type Datagram struct
      • func NewDatagram(atyp byte, dstaddr []byte, dstport []byte, data []byte)
      • func NewDatagramFromBytes(bb []byte)
      • func (d *Datagram) Bytes()

Advanced API

This can satisfy the classic scenario, and it is still recommended that you choose the above small API to customize for special scenarios.

Server: support both TCP and UDP

  • type Server struct
  • type Handler interface
    • TCPHandle(*Server, *net.TCPConn, *Request) error
    • UDPHandle(*Server, *net.UDPAddr, *Datagram) error

Example:

server, _ := NewClassicServer(addr, ip, username, password, tcpTimeout, udpTimeout)
server.ListenAndServe(Handler)

Client: support both TCP and UDP and return net.Conn

  • type Client struct

Example:

client, _ := socks5.NewClient(server, username, password, tcpTimeout, udpTimeout)
conn, _ := client.Dial(network, addr)

Projects using this library

License

Licensed under The MIT License

socks5's People

Contributors

aus avatar cameronelliott avatar mzz2017 avatar onoketa avatar txthinking avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

socks5's Issues

关于参数不太理解

NewClassicServer(addr, ip, username, password, tcpTimeout, udpTimeout)

这个接口即有addr,又有ip 。能说明设计之初,两者之间的区别么

NAT1 Full cone question

Describe actual behavior

I am working on changing the socks5 of this project to full cone(nat1). I would like to ask, can I only receive data packets from remote addr in the udp connection state? If a connect udp is established, such as 127.0.0.1:8003- >1.1.1.1:53, but I want to receive 1.1.1.2:33->127.0.0.1:8003 udp packets, can I only use the non-connected listening port to achieve this?

What is your expected behavior

nat1

Specifications like the version of the project, operating system, or hardware

Steps to reproduce the problem

custom handler?

func ExampleServer() {
s, err := socks5.NewClassicServer("127.0.0.1:1080", "127.0.0.1", "", "", 0, 60)
if err != nil {
log.Println(err)
return
}
// You can pass in custom Handler
s.ListenAndServe(nil)
// #Output:
}

I'm new, can you give an example or clarification in passing a custom Handler?

Send UDP ASSOCIATE command, DST.ADDR and DST.PORT should use zeros.

Describe actual behavior

When request the udp association command, the dst.addr and dst.port is set the destination address and the destination port.

What is your expected behavior

If the client is not in possesion of the information at the time of the UDP ASSOCIATE, the client MUST use a port number and address of all zeros. Come from the describe of UDP ASSOCIATE in rfc-1928

Specifications like the version of the project, operating system, or hardware

Steps to reproduce the problem

package main

import (
	"fmt"

	"github.com/txthinking/socks5"
)

func main() {
	server := "xxx"
	username := "xxx"
	password := "xxx"
	tcpTimeout := 10
	udpTimeout := 60

	client, err := socks5.NewClient(server, username, password, tcpTimeout, udpTimeout)
	if err != nil {
		return
	}

	network := "udp"
	addr := "xxx"
	conn, _ := client.Dial(network, addr)
	_, err = conn.Write([]byte("hello"))
	if err != nil {
		return
	}
	udpResp := make([]byte, 1024)

	_, err = conn.Read(udpResp)
	if err != nil {
		fmt.Println("Error receiving UDP data:", err)
		return
	}
	fmt.Println(udpResp)
}

When using udp connection, some VPS servers can fail due to IP conversion.

I know that UDP connections should use the DST.ADDR and DST.PORT returned by the server to establish a new UDP connection to send data, but building a socks5 server using the servers of some cloud server providers creates a problem:
The VPS provided by these cloud servers use an internal IP(like 10.0.2.*) and an external IP, and they are bound together through the firewall of the cloud provider. In the VPS, only the internal IP can be accessed, so when building the socks5 server, it cannot listen to the external IP and can only return the internal IP to the client. The client then cannot establish a connection properly. This problem does not exist when using TCP.
So I think the best solution to this problem is to set a parameter on the server side to allow custom return of UDP address.

how do you limit access to the udp association if the client is in a LAN?

In RFC 1928, it says:

The DST.ADDR and DST.PORT fields contain the address and port that the client expects to use to send UDP datagrams on for the association. The server MAY use this information to limit access to the association.

But if the client is in a LAN, the server would get a totally different addr and port when receiving udp packets because of the NAT.Then how could i limit access to the udp association?

报错Invalid Version

PC运行server。使用iPhone配置代理地址后server报错invalid version是什么原因呢师傅

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.