Skip to content

Commit

Permalink
server ok
Browse files Browse the repository at this point in the history
  • Loading branch information
txthinking committed Nov 5, 2016
1 parent e4bf265 commit 8257e9d
Show file tree
Hide file tree
Showing 9 changed files with 537 additions and 403 deletions.
7 changes: 0 additions & 7 deletions auth.go

This file was deleted.

269 changes: 140 additions & 129 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,165 +1,176 @@
package socks5

import (
"errors"
"io"
"errors"
"io"
"log"
)

var (
ERROR_BAD_REPLY = errors.New("Bad Reply")
ERROR_BAD_REPLY = errors.New("Bad Reply")
)

type Client struct {
}

func NewNegotiationRequest(methods []byte) *NegotiationRequest {
return &NegotiationRequest{
Ver: VER,
NMethods: byte(len(methods)),
Methods: methods,
}
return &NegotiationRequest{
Ver: VER,
NMethods: byte(len(methods)),
Methods: methods,
}
}

func (r *NegotiationRequest) Write(w io.Writer) error {
if _, err := w.Write([]byte{r.Ver}); err != nil {
return err
}
if _, err := w.Write([]byte{r.NMethods}); err != nil {
return err
}
if _, err := w.Write(r.Methods); err != nil {
return err
}
return nil
if _, err := w.Write([]byte{r.Ver}); err != nil {
return err
}
if _, err := w.Write([]byte{r.NMethods}); err != nil {
return err
}
if _, err := w.Write(r.Methods); err != nil {
return err
}
log.Printf("Sent NegotiationRequest: %#v %#v %#v\n", r.Ver, r.NMethods, r.Methods)
return nil
}

func NewNegotiationReplyFrom(r io.Reader) (*NegotiationReply, error) {
bb := make([]byte, 2)
if _, err := io.ReadFull(r, bb); err != nil {
return nil, err
}
if bb[0] != VER {
return nil, ERROR_VERSION
}
return &NegotiationReply{
Ver: bb[0],
Method: bb[1],
}, nil
bb := make([]byte, 2)
if _, err := io.ReadFull(r, bb); err != nil {
return nil, err
}
if bb[0] != VER {
return nil, ERROR_VERSION
}
log.Printf("Got NegotiationReply: %#v %#v\n", bb[0], bb[1])
return &NegotiationReply{
Ver: bb[0],
Method: bb[1],
}, nil
}

func NewUserPassNegotiationRequest(username []byte, password []byte) *UserPassNegotiationRequest {
return &UserPassNegotiationRequest{
Ver: USER_PASS_VER,
Ulen: byte(len(username)),
Uname: username,
Plen: byte(len(password)),
Passwd: password,
}
return &UserPassNegotiationRequest{
Ver: USER_PASS_VER,
Ulen: byte(len(username)),
Uname: username,
Plen: byte(len(password)),
Passwd: password,
}
}

func (r *UserPassNegotiationRequest) WriteTo(w io.Writer) error {
if _, err := w.Write([]byte{r.Ver, r.Ulen}); err != nil {
return err
}
if _, err := w.Write(r.Uname); err != nil {
return err
}
if _, err := w.Write([]byte{r.Plen}); err != nil {
return err
}
if _, err := w.Write(r.Passwd); err != nil {
return err
}
return nil
if _, err := w.Write([]byte{r.Ver, r.Ulen}); err != nil {
return err
}
if _, err := w.Write(r.Uname); err != nil {
return err
}
if _, err := w.Write([]byte{r.Plen}); err != nil {
return err
}
if _, err := w.Write(r.Passwd); err != nil {
return err
}
log.Printf("Sent UserNameNegotiationRequest: %#v %#v %#v %#v %#v\n", r.Ver, r.Ulen, r.Uname, r.Plen, r.Passwd)
return nil
}

func NewUserPassNegotiationReplyFrom(r io.Reader) (*UserPassNegotiationReply, error) {
bb := make([]byte, 2)
if _, err := io.ReadFull(r, bb); err != nil {
return nil, err
}
if bb[0] != USER_PASS_VER {
return nil, ERROR_USER_PASS_VERSION
}
return &UserPassNegotiationReply{
Ver: bb[0],
Status: bb[1],
}, nil
bb := make([]byte, 2)
if _, err := io.ReadFull(r, bb); err != nil {
return nil, err
}
if bb[0] != USER_PASS_VER {
return nil, ERROR_USER_PASS_VERSION
}
log.Printf("Got UserPassNegotiationReply: %#v %#v \n", bb[0], bb[1])
return &UserPassNegotiationReply{
Ver: bb[0],
Status: bb[1],
}, nil
}

func NewRequest(cmd byte, atyp byte, addr []byte, port []byte) *Request {
return &Request{
Ver: VER,
Cmd: cmd,
Rsv: 0x00,
Atyp: atyp,
DstAddr: addr,
DstPort: port,
}
func NewRequest(cmd byte, atyp byte, dstaddr []byte, dstport []byte) *Request {
if atyp == ATYP_DOMAIN {
dstaddr = append([]byte{byte(len(dstaddr))}, dstaddr...)
}
return &Request{
Ver: VER,
Cmd: cmd,
Rsv: 0x00,
Atyp: atyp,
DstAddr: dstaddr,
DstPort: dstport,
}
}

func (r *Request) WriteTo(w io.Writer) error {
if _, err := w.Write([]byte{r.Ver, r.Cmd, r.Rsv, r.Atyp}); err != nil {
return err
}
if r.Atyp == ATYP_DOMAIN {
if _, err := w.Write([]byte{byte(len(r.DstAddr))}); err != nil {
return err
}
}
if _, err := w.Write(r.DstAddr); err != nil {
return err
}
if _, err := w.Write(r.DstPort); err != nil {
return err
}
return nil
if _, err := w.Write([]byte{r.Ver, r.Cmd, r.Rsv, r.Atyp}); err != nil {
return err
}
if r.Atyp == ATYP_DOMAIN {
if _, err := w.Write([]byte{byte(len(r.DstAddr))}); err != nil {
return err
}
}
if _, err := w.Write(r.DstAddr); err != nil {
return err
}
if _, err := w.Write(r.DstPort); err != nil {
return err
}
log.Printf("Sent Request: %#v %#v %#v %#v %#v %#v\n", r.Ver, r.Cmd, r.Rsv, r.Atyp, r.DstAddr, r.DstPort)
return nil
}

func NewReplyFrom(r io.Reader) (*Reply, error) {
bb := make([]byte, 4)
if _, err := io.ReadFull(r, bb); err != nil {
return nil, err
}
if bb[0] != VER {
return nil, ERROR_VERSION
}
var addr []byte
if bb[3] == ATYP_IPV4 {
addr = make([]byte, 4)
if _, err := io.ReadFull(r, addr); err != nil {
return nil, err
}
} else if bb[3] == ATYP_IPV6 {
addr = make([]byte, 16)
if _, err := io.ReadFull(r, addr); err != nil {
return nil, err
}
} else if bb[3] == ATYP_DOMAIN {
dal := make([]byte, 1)
if _, err := io.ReadFull(r, dal); err != nil {
return nil, err
}
if dal[0] == 0 {
return nil, ERROR_BAD_REPLY
}
addr = make([]byte, int(dal[0]))
if _, err := io.ReadFull(r, addr); err != nil {
return nil, err
}
} else {
return nil, ERROR_BAD_REPLY
}
port := make([]byte, 2)
if _, err := io.ReadFull(r, port); err != nil {
return nil, err
}
return &Reply{
Ver: bb[0],
Rep: bb[1],
Rsv: bb[2],
Atyp: bb[3],
BndAddr: addr,
BndPort: port,
}, nil
bb := make([]byte, 4)
if _, err := io.ReadFull(r, bb); err != nil {
return nil, err
}
if bb[0] != VER {
return nil, ERROR_VERSION
}
var addr []byte
if bb[3] == ATYP_IPV4 {
addr = make([]byte, 4)
if _, err := io.ReadFull(r, addr); err != nil {
return nil, err
}
} else if bb[3] == ATYP_IPV6 {
addr = make([]byte, 16)
if _, err := io.ReadFull(r, addr); err != nil {
return nil, err
}
} else if bb[3] == ATYP_DOMAIN {
dal := make([]byte, 1)
if _, err := io.ReadFull(r, dal); err != nil {
return nil, err
}
if dal[0] == 0 {
return nil, ERROR_BAD_REPLY
}
addr = make([]byte, int(dal[0]))
if _, err := io.ReadFull(r, addr); err != nil {
return nil, err
}
addr = append(dal, addr...)
} else {
return nil, ERROR_BAD_REPLY
}
port := make([]byte, 2)
if _, err := io.ReadFull(r, port); err != nil {
return nil, err
}
log.Printf("Got Reply: %#v %#v %#v %#v %#v %#v\n", bb[0], bb[1], bb[2], bb[3], addr, port)
return &Reply{
Ver: bb[0],
Rep: bb[1],
Rsv: bb[2],
Atyp: bb[3],
BndAddr: addr,
BndPort: port,
}, nil
}
66 changes: 37 additions & 29 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
package main

import (
"log"
"net"
"time"

"github.com/txthinking/socks5"
"log"
"net"
//"time"
"github.com/txthinking/socks5"
"net/http"
_ "net/http/pprof"
)

func main() {
l, err := net.Listen("tcp", ":1090")
if err != nil {
log.Fatal(err)
}
defer func() {
if err := l.Close(); err != nil {
log.Println(err)
}
}()
for {
c, err := l.Accept()
if err != nil {
log.Fatal(err)
}
if err := c.SetDeadline(time.Now().Add(10 * time.Second)); err != nil {
log.Println(err)
if err = c.Close(); err != nil {
log.Println(err)
}
continue
}
s := socks5.NewServer(c)
go s.Handle()
}
go func (){
log.Println(http.ListenAndServe(":1094", nil))
}()
l, err := net.Listen("tcp", ":1090")
if err != nil {
log.Fatal(err)
}
defer func() {
if err := l.Close(); err != nil {
log.Println(err)
}
}()
for {
c, err := l.Accept()
if err != nil {
log.Fatal(err)
}
//if err := c.SetDeadline(time.Now().Add(60 * time.Second)); err != nil {
//log.Println(err)
//if err = c.Close(); err != nil {
//log.Println(err)
//}
//continue
//}
s := socks5.NewServer(c)
go func (){
if err := s.Handle(); err != nil {
log.Println(err)
}
}()
}
}
Loading

0 comments on commit 8257e9d

Please sign in to comment.