Skip to content

Commit

Permalink
proxy: copypasta proxy into here for fast fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Pashmfouroush <[email protected]>
  • Loading branch information
markpash committed Mar 14, 2024
1 parent 795f039 commit 2d777a8
Show file tree
Hide file tree
Showing 18 changed files with 2,181 additions and 11 deletions.
6 changes: 3 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func runWarp(ctx context.Context, l *slog.Logger, bind netip.AddrPort, endpoint
}

tnet.StartProxy(bind)
l.Info("Serving proxy", "address", bind)
l.Info("serving proxy", "address", bind)

return nil
}
Expand Down Expand Up @@ -155,7 +155,7 @@ func runWarpWithPsiphon(ctx context.Context, l *slog.Logger, bind netip.AddrPort
return fmt.Errorf("unable to run psiphon %w", err)
}

l.Info("Serving proxy", "address", bind)
l.Info("serving proxy", "address", bind)

return nil
}
Expand Down Expand Up @@ -210,7 +210,7 @@ func runWarpInWarp(ctx context.Context, l *slog.Logger, bind netip.AddrPort, end

tnet.StartProxy(bind)

l.Info("Serving proxy", "address", bind)
l.Info("serving proxy", "address", bind)
return nil
}

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ replace github.com/Psiphon-Labs/psiphon-tunnel-core => github.com/bepass-org/psi

require (
github.com/Psiphon-Labs/psiphon-tunnel-core v2.0.28+incompatible
github.com/bepass-org/proxy v0.0.0-20240201095508-c86216dd0aea
github.com/fatih/color v1.16.0
github.com/flynn/noise v1.1.0
github.com/frankban/quicktest v1.14.6
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/armon/go-proxyproto v0.0.0-20180202201750-5b7edb60ff5f h1:SaJ6yqg936TshyeFZqQE+N+9hYkIeL9AMr7S4voCl10=
github.com/armon/go-proxyproto v0.0.0-20180202201750-5b7edb60ff5f/go.mod h1:QmP9hvJ91BbJmGVGSbutW19IC0Q9phDCLGaomwTJbgU=
github.com/bepass-org/proxy v0.0.0-20240201095508-c86216dd0aea h1:6GKkjxDUxqq7uwA8U15N4PFURhdNN0OrxFuXc58MGUU=
github.com/bepass-org/proxy v0.0.0-20240201095508-c86216dd0aea/go.mod h1:RlF0oO3D6Ju6VYjtL1I6lVLdc3l8jA4ggleJc8S+P0Y=
github.com/bepass-org/psiphon-tunnel-core v0.0.0-20240311155012-9c2e10df08e5 h1:UVdsUQXhviRMzVA02BGzEHUYUBAAeSJYijqKWJvMCxs=
github.com/bepass-org/psiphon-tunnel-core v0.0.0-20240311155012-9c2e10df08e5/go.mod h1:vA5iCui7nfavWyBN8MsLYZ5xpKItjrTvPC0SuMWz48Q=
github.com/bifurcation/mint v0.0.0-20180306135233-198357931e61 h1:BU+NxuoaYPIvvp8NNkNlLr8aA0utGyuunf4Q3LJ0bh0=
Expand Down
67 changes: 67 additions & 0 deletions proxy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Table of Contents
- [Introduction](#introduction)
- [Features](#features)
- [Installation](#installation)
- [Examples](#examples)
- [Minimal](#minimal)
- [Customized](#customized)


## Introduction
The proxy module simplifies connection handling and offers a generic way to work with both HTTP and SOCKS connections,
making it a powerful tool for managing network traffic.


## Features
The Inbound Proxy project offers the following features:

- Full support for `HTTP`, `SOCKS5`, `SOCKS5h`, `SOCKS4` and `SOCKS4a` protocols.
- Handling of `HTTP` and `HTTPS-connect` proxy requests.
- Full support for both `IPv4` and `IPv6`.
- Able to handle both `TCP` and `UDP` traffic.

## Installation

```bash
go get github.com/bepass-org/proxy
```

### Examples

#### Minimal

```go
package main

import (
"github.com/bepass-org/proxy/pkg/mixed"
)

func main() {
proxy := mixed.NewProxy()
_ = proxy.ListenAndServe()
}
```

#### Customized

```go
package main

import (
"github.com/bepass-org/proxy/pkg/mixed"
)

func main() {
proxy := mixed.NewProxy(
mixed.WithBindAddress("0.0.0.0:8080"),
)
_ = proxy.ListenAndServe()
}

```

There are other examples provided in the [example](https://github.com/bepass-org/proxy/tree/main/example) directory



35 changes: 35 additions & 0 deletions proxy/example/customHandler/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"fmt"
"io"
"log"
"net"

"github.com/bepass-org/warp-plus/proxy/pkg/mixed"
"github.com/bepass-org/warp-plus/proxy/pkg/statute"
)

func main() {
proxy := mixed.NewProxy(
mixed.WithBindAddress("127.0.0.1:1080"),
mixed.WithUserHandler(generalHandler),
)
_ = proxy.ListenAndServe()
}

func generalHandler(req *statute.ProxyRequest) error {
fmt.Println("handling request to", req.Destination)
conn, err := net.Dial(req.Network, req.Destination)
if err != nil {
return err
}
go func() {
_, err := io.Copy(conn, req.Conn)
if err != nil {
log.Println(err)
}
}()
_, err = io.Copy(req.Conn, conn)
return err
}
10 changes: 10 additions & 0 deletions proxy/example/minimal/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"github.com/bepass-org/warp-plus/proxy/pkg/mixed"
)

func main() {
proxy := mixed.NewProxy()
_ = proxy.ListenAndServe()
}
69 changes: 69 additions & 0 deletions proxy/example/udpClient/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"encoding/binary"
"fmt"
"io"
"net"
"strconv"
)

func main() {
proxyAddr := "127.0.0.1:1080"
targetAddr := "<YOUR Netcat ip address>:4444"

// Connect to SOCKS5 proxy
conn, err := net.Dial("tcp", proxyAddr)
if err != nil {
panic(err)
}
defer conn.Close()

// Send greeting to SOCKS5 proxy
conn.Write([]byte{0x05, 0x01, 0x00})

// Read greeting response
response := make([]byte, 2)
io.ReadFull(conn, response)

// Send UDP ASSOCIATE request
conn.Write([]byte{0x05, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})

// Read UDP ASSOCIATE response
response = make([]byte, 10)
io.ReadFull(conn, response)

// Extract the bind address and port
bindIP := net.IP(response[4:8])
bindPort := binary.BigEndian.Uint16(response[8:10])

// Print the bind address
fmt.Printf("Bind address: %s:%d\n", bindIP, bindPort)

// Create UDP connection
udpConn, err := net.Dial("udp", fmt.Sprintf("%s:%d", bindIP, bindPort))
if err != nil {
panic(err)
}
defer udpConn.Close()

// Extract target IP and port
dstIP, dstPortStr, _ := net.SplitHostPort(targetAddr)
dstPort, _ := strconv.Atoi(dstPortStr)

// Construct the UDP packet with the target address and message
packet := make([]byte, 0)
packet = append(packet, 0x00, 0x00, 0x00) // RSV and FRAG
packet = append(packet, 0x01) // ATYP for IPv4
packet = append(packet, net.ParseIP(dstIP).To4()...)
packet = append(packet, byte(dstPort>>8), byte(dstPort&0xFF))
packet = append(packet, []byte("Hello, UDP through SOCKS5!")...)

// Send the UDP packet
udpConn.Write(packet)

// Read the response
buffer := make([]byte, 1024)
n, _ := udpConn.Read(buffer)
fmt.Println("Received:", string(buffer[10:n]))
}
84 changes: 84 additions & 0 deletions proxy/pkg/http/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package http

import (
"bytes"
"fmt"
"net"
"net/http"
"sync"
)

// copyBuffer is a helper function to copy data between two net.Conn objects.
// func copyBuffer(dst, src net.Conn, buf []byte) (int64, error) {
// return io.CopyBuffer(dst, src, buf)
// }

type responseWriter struct {
conn net.Conn
headers http.Header
status int
written bool
}

func NewHTTPResponseWriter(conn net.Conn) http.ResponseWriter {
return &responseWriter{
conn: conn,
headers: http.Header{},
status: http.StatusOK,
}
}

func (rw *responseWriter) Header() http.Header {
return rw.headers
}

func (rw *responseWriter) WriteHeader(statusCode int) {
if rw.written {
return
}
rw.status = statusCode
rw.written = true

statusText := http.StatusText(statusCode)
if statusText == "" {
statusText = fmt.Sprintf("status code %d", statusCode)
}
_, _ = fmt.Fprintf(rw.conn, "HTTP/1.1 %d %s\r\n", statusCode, statusText)
_ = rw.headers.Write(rw.conn)
_, _ = rw.conn.Write([]byte("\r\n"))
}

func (rw *responseWriter) Write(data []byte) (int, error) {
if !rw.written {
rw.WriteHeader(http.StatusOK)
}
return rw.conn.Write(data)
}

type customConn struct {
net.Conn
req *http.Request
initialData []byte
once sync.Once
}

func (c *customConn) Read(p []byte) (n int, err error) {
c.once.Do(func() {
buf := &bytes.Buffer{}
err = c.req.Write(buf)
if err != nil {
n = 0
return
}
c.initialData = buf.Bytes()
})

if len(c.initialData) > 0 {
copy(p, c.initialData)
n = len(p)
c.initialData = nil
return
}

return c.Conn.Read(p)
}
Loading

0 comments on commit 2d777a8

Please sign in to comment.