Skip to content

Commit

Permalink
Optimize code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Oct 2, 2024
1 parent fa48414 commit af079d1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
37 changes: 18 additions & 19 deletions protocol/ashe/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (
"github.com/mohanson/daze/lib/doa"
)

// This document describes a TCP-based cryptographic proxy protocol. The main purpose of this protocol is to bypass
// This document describes a tcp-based cryptographic proxy protocol. The main purpose of this protocol is to bypass
// firewalls while providing a good user experience, so it only provides minimal security, which is one of the reasons
// for choosing the RC4 algorithm(RC4 is cryptographically broken and should not be used for secure applications).
// for choosing the rc4 algorithm(rc4 is cryptographically broken and should not be used for secure applications).
//
// The client connects to the server, and sends a request details:
//
// +------+------+-----+---------+---------+
// | Salt | Time | Net | DST.Len | DST |
// | Salt | Time | Net | Dst.Len | Dst |
// +------+------+-----+---------+---------+
// | 128 | 8 | 1 | 1 | 0 - 255 |
// +------+------+-----+---------+---------+
Expand All @@ -31,8 +31,8 @@ import (
// attacks
// - Net : 0x01 : TCP
// 0x03 : UDP
// - DST.Len : Len of DST
// - DST : Desired destination address
// - Dst.Len : Destination address's length
// - Dst : Destination address
//
// The server returns:
//
Expand All @@ -42,8 +42,8 @@ import (
// | 1 |
// +------+
//
// - Code: 0x00: succeed
// 0x01: general server failure
// - Code: 0x00: Succeed.
// 0x01: General server failure

// Conf is acting as package level configuration.
var Conf = struct {
Expand All @@ -53,7 +53,7 @@ var Conf = struct {
LifeExpired: 120,
}

// TCPConn is an implementation of the Conn interface for TCP network connections.
// TCPConn is an implementation of the Conn interface for tcp network connections.
type TCPConn struct {
io.ReadWriteCloser
}
Expand All @@ -63,7 +63,7 @@ func NewTCPConn(c io.ReadWriteCloser) *TCPConn {
return &TCPConn{c}
}

// UDPConn is an implementation of the Conn interface for UDP network connections.
// UDPConn is an implementation of the Conn interface for udp network connections.
type UDPConn struct {
io.ReadWriteCloser
b []byte
Expand All @@ -74,7 +74,7 @@ func NewUDPConn(c io.ReadWriteCloser) *UDPConn {
return &UDPConn{ReadWriteCloser: c, b: make([]byte, 2)}
}

// Read implements the Conn Read method.
// Read reads up to len(p) bytes into p.
func (c *UDPConn) Read(p []byte) (int, error) {
_, err := io.ReadFull(c.ReadWriteCloser, p[:2])
if err != nil {
Expand All @@ -84,12 +84,11 @@ func (c *UDPConn) Read(p []byte) (int, error) {
return io.ReadFull(c.ReadWriteCloser, p[:n])
}

// Write implements the Conn Write method.
// Write writes len(p) bytes from p to the underlying data stream.
func (c *UDPConn) Write(p []byte) (int, error) {
// Maximum UDP packet size is 2^16 bytes in theoretically.
// But every packet lives in an Ethernet frame. Ethernet frames can only contain 1500 bytes of data. This is called
// the "maximum transmission unit" or "MTU".
doa.Doa(len(p) <= math.MaxUint16)
// Maximum udp payload size is 65527(equal to 65535 - 8) bytes in theoretically. The 8 in the formula means the udp
// header, which contains source port, destination port, length and checksum.
doa.Doa(len(p) <= 65527)
binary.BigEndian.PutUint16(c.b, uint16(len(p)))
_, err := c.ReadWriteCloser.Write(c.b[:2])
if err != nil {
Expand All @@ -98,7 +97,7 @@ func (c *UDPConn) Write(p []byte) (int, error) {
return c.ReadWriteCloser.Write(p)
}

// Server implemented the ashe protocol. The ASHE server will typically evaluate the request based on source and
// Server implemented the ashe protocol. The ashe server will typically evaluate the request based on source and
// destination addresses, and return one or more reply messages, as appropriate for the request type.
type Server struct {
// Cipher is a pre-shared key.
Expand Down Expand Up @@ -231,7 +230,7 @@ func (s *Server) Run() error {
return nil
}

// NewServer returns a new Server.
// NewServer returns a new Server. Cipher is a password in string form, with no length limit.
func NewServer(listen string, cipher string) *Server {
return &Server{
Listen: listen,
Expand Down Expand Up @@ -271,7 +270,7 @@ func (c *Client) Hello(srv io.ReadWriteCloser) (io.ReadWriteCloser, error) {
return con, nil
}

// Establish an existing connection. It is the caller's responsibility to close the con.
// Establish an existing connection. It is the caller's responsibility to close the conn.
func (c *Client) Estab(ctx *daze.Context, srv io.ReadWriteCloser, network string, address string) (io.ReadWriteCloser, error) {
var (
buf = make([]byte, 2)
Expand Down Expand Up @@ -334,7 +333,7 @@ func (c *Client) Dial(ctx *daze.Context, network string, address string) (io.Rea
return con, err
}

// NewClient returns a new Client. A secret data needs to be passed in Cipher, as a sign to interface with the Server.
// NewClient returns a new Client. Cipher is a password in string form, with no length limit.
func NewClient(server, cipher string) *Client {
return &Client{
Server: server,
Expand Down
8 changes: 4 additions & 4 deletions protocol/baboon/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/mohanson/daze/protocol/ashe"
)

// Protocol baboon is the ashe protocol based on HTTP.
// Protocol baboon is the ashe protocol based on http.

// Conf is acting as package level configuration.
var Conf = struct {
Expand Down Expand Up @@ -60,7 +60,7 @@ func (s *Server) ServeMask(w http.ResponseWriter, r *http.Request) {
io.Copy(w, ret.Body)
}

// ServeDaze degenerate HTTP protocol and run ashe protocol on it.
// ServeDaze degenerate http protocol and run ashe protocol on it.
func (s *Server) ServeDaze(w http.ResponseWriter, r *http.Request) {
hj, _ := w.(http.Hijacker)
cc, rw, _ := hj.Hijack()
Expand Down Expand Up @@ -139,7 +139,7 @@ func (s *Server) Run() error {
return nil
}

// NewServer returns a new Server.
// NewServer returns a new Server. Cipher is a password in string form, with no length limit.
func NewServer(listen string, cipher string) *Server {
return &Server{
Cipher: daze.Salt(cipher),
Expand Down Expand Up @@ -184,7 +184,7 @@ func (c *Client) Dial(ctx *daze.Context, network string, address string) (io.Rea
return con, err
}

// NewClient returns a new Client.
// NewClient returns a new Client. Cipher is a password in string form, with no length limit.
func NewClient(server string, cipher string) *Client {
return &Client{
Cipher: daze.Salt(cipher),
Expand Down
8 changes: 4 additions & 4 deletions protocol/czar/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/mohanson/daze/protocol/ashe"
)

// The czar protocol is a proxy protocol built on TCP multiplexing technology. By establishing multiple TCP connections
// in one TCP channel, czar protocol effectively reduces the consumption of establishing connections between the client
// The czar protocol is a proxy protocol built on tcp multiplexing technology. By establishing multiple tcp connections
// in one tcp channel, czar protocol effectively reduces the consumption of establishing connections between the client
// and the server:
//
// Client port: a.com ------------┐ ┌------------ Server port: a.com
Expand Down Expand Up @@ -103,7 +103,7 @@ func (s *Server) Run() error {
return nil
}

// NewServer returns a new Server.
// NewServer returns a new Server. Cipher is a password in string form, with no length limit.
func NewServer(listen string, cipher string) *Server {
return &Server{
Cipher: daze.Salt(cipher),
Expand Down Expand Up @@ -192,7 +192,7 @@ func (c *Client) Run() {
}
}

// NewClient returns a new Client.
// NewClient returns a new Client. Cipher is a password in string form, with no length limit.
func NewClient(server, cipher string) *Client {
client := &Client{
Cancel: make(chan struct{}),
Expand Down
4 changes: 2 additions & 2 deletions protocol/dahlia/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (s *Server) Run() error {
return nil
}

// NewServer returns a new Server.
// NewServer returns a new Server. Cipher is a password in string form, with no length limit.
func NewServer(listen string, server string, cipher string) *Server {
return &Server{
Cipher: daze.Salt(cipher),
Expand Down Expand Up @@ -154,7 +154,7 @@ func (c *Client) Run() error {
return nil
}

// NewClient returns a new Client.
// NewClient returns a new Client. Cipher is a password in string form, with no length limit.
func NewClient(listen string, server string, cipher string) *Client {
return &Client{
Cipher: daze.Salt(cipher),
Expand Down

0 comments on commit af079d1

Please sign in to comment.