Skip to content

Commit

Permalink
Variable name optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Aug 6, 2024
1 parent c0e47c5 commit 2b85ab1
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 81 deletions.
76 changes: 38 additions & 38 deletions protocol/ashe/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,31 +101,31 @@ func (c *UDPConn) Write(p []byte) (int, error) {
// 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 {
Listen string
// Cipher is a pre-shared key.
Cipher []byte
Closer io.Closer
Listen string
}

// Hello creates an encrypted channel.
func (s *Server) Hello(con io.ReadWriteCloser) (io.ReadWriteCloser, error) {
func (s *Server) Hello(cli io.ReadWriteCloser) (io.ReadWriteCloser, error) {
var (
buf = make([]byte, 32)
cli io.ReadWriteCloser
con io.ReadWriteCloser
err error
gap int64
gapSign int64
)
_, err = io.ReadFull(con, buf[:])
_, err = io.ReadFull(cli, buf[:])
if err != nil {
return nil, err
}
// To build a key from pre-shared key. Use xor as our key derivation function.
for i := range 32 {
buf[i] ^= s.Cipher[i]
}
cli = daze.Gravity(con, buf[:])
_, err = io.ReadFull(cli, buf[:8])
con = daze.Gravity(cli, buf[:])
_, err = io.ReadFull(con, buf[:8])
if err != nil {
return nil, err
}
Expand All @@ -136,31 +136,31 @@ func (s *Server) Hello(con io.ReadWriteCloser) (io.ReadWriteCloser, error) {
if gap^gapSign-gapSign > int64(Conf.LifeExpired) {
return nil, errors.New("daze: request expired")
}
return cli, nil
return con, nil
}

// Serve incoming connections. Parameter cli will be closed automatically when the function exits.
func (s *Server) Serve(ctx *daze.Context, con io.ReadWriteCloser) error {
func (s *Server) Serve(ctx *daze.Context, cli io.ReadWriteCloser) error {
var (
buf = make([]byte, 256)
cli io.ReadWriteCloser
con io.ReadWriteCloser
dst string
dstLen uint8
dstNet uint8
srv io.ReadWriteCloser
err error
srv io.ReadWriteCloser
)
cli, err = s.Hello(con)
con, err = s.Hello(cli)
if err != nil {
return err
}
_, err = io.ReadFull(cli, buf[:2])
_, err = io.ReadFull(con, buf[:2])
if err != nil {
return err
}
dstNet = buf[0]
dstLen = buf[1]
_, err = io.ReadFull(cli, buf[:dstLen])
_, err = io.ReadFull(con, buf[:dstLen])
if err != nil {
return err
}
Expand All @@ -174,17 +174,17 @@ func (s *Server) Serve(ctx *daze.Context, con io.ReadWriteCloser) error {
srv, err = daze.Dial("udp", dst)
}
if err != nil {
cli.Write([]byte{1})
con.Write([]byte{1})
return err
}
cli.Write([]byte{0})
con.Write([]byte{0})
switch dstNet {
case 0x01:
cli = NewTCPConn(cli)
con = NewTCPConn(con)
case 0x03:
cli = NewUDPConn(cli)
con = NewUDPConn(con)
}
daze.Link(cli, srv)
daze.Link(con, srv)
return nil
}

Expand Down Expand Up @@ -218,13 +218,13 @@ func (s *Server) Run() error {
idx++
ctx := &daze.Context{Cid: idx}
log.Printf("conn: %08x accept remote=%s", ctx.Cid, cli.RemoteAddr())
go func(ctx *daze.Context, cli net.Conn) {
go func() {
defer cli.Close()
if err := s.Serve(ctx, cli); err != nil {
log.Printf("conn: %08x error %s", ctx.Cid, err)
}
log.Printf("conn: %08x closed", ctx.Cid)
}(ctx, cli)
}()
}
}()

Expand All @@ -241,51 +241,51 @@ func NewServer(listen string, cipher string) *Server {

// Client implemented the ashe protocol.
type Client struct {
Server string
// Cipher is a pre-shared key.
Cipher []byte
Server string
}

// Hello creates an encrypted channel.
func (c *Client) Hello(con io.ReadWriteCloser) (io.ReadWriteCloser, error) {
func (c *Client) Hello(srv io.ReadWriteCloser) (io.ReadWriteCloser, error) {
var (
buf = make([]byte, 32)
con io.ReadWriteCloser
err error
srv io.ReadWriteCloser
)
io.ReadFull(&daze.RandomReader{}, buf[:])
_, err = con.Write(buf[:])
_, err = srv.Write(buf[:])
if err != nil {
return nil, err
}
// To build a key from pre-shared key. Use xor as our key derivation function.
for i := range 32 {
buf[i] ^= c.Cipher[i]
}
srv = daze.Gravity(con, buf[:])
con = daze.Gravity(srv, buf[:])
binary.BigEndian.PutUint64(buf[:8], uint64(time.Now().Unix()))
_, err = srv.Write(buf[:8])
_, err = con.Write(buf[:8])
if err != nil {
return nil, err
}
return srv, nil
return con, nil
}

// Establish an existing connection. It is the caller's responsibility to close the con.
func (c *Client) Estab(ctx *daze.Context, con io.ReadWriteCloser, network string, address string) (io.ReadWriteCloser, error) {
func (c *Client) Estab(ctx *daze.Context, srv io.ReadWriteCloser, network string, address string) (io.ReadWriteCloser, error) {
var (
n = len(address)
buf = make([]byte, 2)
con io.ReadWriteCloser
err error
srv io.ReadWriteCloser
n = len(address)
)
if n > 255 {
return nil, fmt.Errorf("daze: destination address too long %s", address)
}
if network != "tcp" && network != "udp" {
return nil, fmt.Errorf("daze: network must be tcp or udp")
}
srv, err = c.Hello(con)
con, err = c.Hello(srv)
if err != nil {
return nil, err
}
Expand All @@ -296,12 +296,12 @@ func (c *Client) Estab(ctx *daze.Context, con io.ReadWriteCloser, network string
buf[0x00] = 0x03
}
buf[0x01] = uint8(n)
srv.Write(buf[:2])
_, err = srv.Write([]byte(address))
con.Write(buf[:2])
_, err = con.Write([]byte(address))
if err != nil {
return nil, err
}
_, err = io.ReadFull(srv, buf[:1])
_, err = io.ReadFull(con, buf[:1])
if err != nil {
return nil, err
}
Expand All @@ -310,9 +310,9 @@ func (c *Client) Estab(ctx *daze.Context, con io.ReadWriteCloser, network string
}
switch network {
case "tcp":
return NewTCPConn(srv), nil
return NewTCPConn(con), nil
case "udp":
return NewUDPConn(srv), nil
return NewUDPConn(con), nil
}
panic("unreachable")
}
Expand All @@ -323,11 +323,11 @@ func (c *Client) Dial(ctx *daze.Context, network string, address string) (io.Rea
if err != nil {
return nil, err
}
ret, err := c.Estab(ctx, srv, network, address)
con, err := c.Estab(ctx, srv, network, address)
if err != nil {
srv.Close()
}
return ret, err
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.
Expand Down
28 changes: 11 additions & 17 deletions protocol/baboon/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ var Conf = struct {

// Server implemented the baboon protocol.
type Server struct {
Listen string
Cipher []byte
Closer io.Closer
Listen string
Masker string
NextID uint32
}
Expand Down Expand Up @@ -74,13 +74,10 @@ func (s *Server) ServeDaze(w http.ResponseWriter, r *http.Request) {
Writer: cc,
Closer: cc,
}
srv := ashe.Server{
Listen: s.Listen,
Cipher: s.Cipher,
}
spy := &ashe.Server{Cipher: s.Cipher}
ctx := &daze.Context{Cid: atomic.AddUint32(&s.NextID, 1)}
log.Printf("conn: %08x accept remote=%s", ctx.Cid, cc.RemoteAddr())
if err := srv.Serve(ctx, cli); err != nil {
if err := spy.Serve(ctx, cli); err != nil {
log.Printf("conn: %08x error %s", ctx.Cid, err)
}
log.Printf("conn: %08x closed", ctx.Cid)
Expand Down Expand Up @@ -145,26 +142,26 @@ func (s *Server) Run() error {
// NewServer returns a new Server.
func NewServer(listen string, cipher string) *Server {
return &Server{
Listen: listen,
Cipher: daze.Salt(cipher),
Listen: listen,
Masker: Conf.Masker,
NextID: uint32(math.MaxUint32),
}
}

// Client implemented the baboon protocol.
type Client struct {
Server string
Cipher []byte
Server string
}

// Dial connects to the address on the named network.
func (c *Client) Dial(ctx *daze.Context, network string, address string) (io.ReadWriteCloser, error) {
var (
srv io.ReadWriteCloser
buf = make([]byte, 256)
req *http.Request
err error
req *http.Request
srv io.ReadWriteCloser
)
srv, err = daze.Dial("tcp", c.Server)
if err != nil {
Expand All @@ -179,21 +176,18 @@ func (c *Client) Dial(ctx *daze.Context, network string, address string) (io.Rea
req.Write(srv)
// Discard responded header
io.ReadFull(srv, buf[:147])
cli := &ashe.Client{
Server: c.Server,
Cipher: c.Cipher,
}
ret, err := cli.Estab(ctx, srv, network, address)
spy := &ashe.Client{Cipher: c.Cipher}
con, err := spy.Estab(ctx, srv, network, address)
if err != nil {
srv.Close()
}
return ret, err
return con, err
}

// NewClient returns a new Client.
func NewClient(server string, cipher string) *Client {
return &Client{
Server: server,
Cipher: daze.Salt(cipher),
Server: server,
}
}
22 changes: 11 additions & 11 deletions protocol/czar/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ import (

// Server implemented the czar protocol.
type Server struct {
Listen string
Cipher []byte
Closer io.Closer
Listen string
}

// Serve incoming connections. Parameter cli will be closed automatically when the function exits.
func (s *Server) Serve(ctx *daze.Context, cli io.ReadWriteCloser) error {
asheServer := &ashe.Server{Cipher: s.Cipher}
return asheServer.Serve(ctx, cli)
spy := &ashe.Server{Cipher: s.Cipher}
return spy.Serve(ctx, cli)
}

// Close listener.
Expand Down Expand Up @@ -89,13 +89,13 @@ func (s *Server) Run() error {
idx++
ctx := &daze.Context{Cid: idx}
log.Printf("conn: %08x accept remote=%s", ctx.Cid, mux.con.RemoteAddr())
go func(ctx *daze.Context, cli io.ReadWriteCloser) {
go func() {
defer cli.Close()
if err := s.Serve(ctx, cli); err != nil {
log.Printf("conn: %08x error %s", ctx.Cid, err)
}
log.Printf("conn: %08x closed", ctx.Cid)
}(ctx, cli)
}()
}
}(mux)
}
Expand All @@ -107,16 +107,16 @@ func (s *Server) Run() error {
// NewServer returns a new Server.
func NewServer(listen string, cipher string) *Server {
return &Server{
Listen: listen,
Cipher: daze.Salt(cipher),
Listen: listen,
}
}

// Client implemented the czar protocol.
type Client struct {
Cipher []byte
Server string
Mux chan *Mux
Server string
}

func (c *Client) Close() error {
Expand All @@ -131,12 +131,12 @@ func (c *Client) Dial(ctx *daze.Context, network string, address string) (io.Rea
if err != nil {
return nil, err
}
asheClient := &ashe.Client{Cipher: c.Cipher}
ret, err := asheClient.Estab(ctx, srv, network, address)
spy := &ashe.Client{Cipher: c.Cipher}
con, err := spy.Estab(ctx, srv, network, address)
if err != nil {
srv.Close()
}
return ret, err
return con, err
case <-time.After(daze.Conf.DialerTimeout):
return nil, fmt.Errorf("dial tcp: %s: i/o timeout", address)
}
Expand Down Expand Up @@ -164,8 +164,8 @@ func (c *Client) Run() {
func NewClient(server, cipher string) *Client {
client := &Client{
Cipher: daze.Salt(cipher),
Server: server,
Mux: make(chan *Mux),
Server: server,
}
go client.Run()
return client
Expand Down
Loading

0 comments on commit 2b85ab1

Please sign in to comment.