forked from WireGuard/wireguard-go
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
proxy: copypasta proxy into here for fast fixes
Signed-off-by: Mark Pashmfouroush <[email protected]>
- Loading branch information
Showing
18 changed files
with
2,181 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.