Skip to content

Commit 7a5b454

Browse files
wuxingzhangnephen
wuxingzhang
authored andcommitted
add readme env
1 parent 13ea56a commit 7a5b454

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

.env.example

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALLOWED_IPS=127.0.0.1,192.168.0.5
2+
PORT = 1080

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,20 @@
11
# go-socks-proxy
22
Go-Socks-Proxy is a simple open-source project written in Go that allows you to connect to the internet through the SOCKS5 protocol. It's ideal for deployment on internal servers since it only handles IP whitelisting and doesn't implement encryption.
3+
4+
## Installation
5+
1. Download the source code and navigate to the directory.
6+
2. Run `go build .` to compile the `go-socks-proxy` file.
7+
3. Configure the `ALLOWED_IPS` and `PORT` variables in the `.env` file.
8+
4. Run `nohup ./go-socks-proxy &` to start the server.
9+
10+
## Usage
11+
Go-Socks-Proxy is easy to use. Simply configure your device's network settings to use the server's IP address and the port number you specified in the `.env` file.
12+
13+
## Security
14+
Please note that Go-Socks-Proxy doesn't provide encryption, so it's not recommended to use it for sensitive data.
15+
16+
## Contributions
17+
Contributions to the project are welcome. Feel free to fork the repository and submit a pull request.
18+
19+
## License
20+
Go-Socks-Proxy is released under the MIT license. See `LICENSE` for more information.

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module go-socks-proxy
22

33
go 1.18
4+
5+
require github.com/joho/godotenv v1.5.1 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
2+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=

main.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import (
99
"io"
1010
"log"
1111
"net"
12+
"os"
1213
"strings"
14+
15+
"github.com/joho/godotenv"
1316
)
1417

1518
const socks5Ver = 0x05
@@ -18,16 +21,27 @@ const atypIPV4 = 0x01
1821
const atypeHOST = 0x03
1922
const atypeIPV6 = 0x04
2023

24+
func init() {
25+
err := godotenv.Load()
26+
if err != nil {
27+
log.Fatalf("Error loading .env file: %v", err)
28+
}
29+
}
30+
2131
func main() {
32+
// 获取 ALLOWED_IPS 环境变量的值
33+
allowedIPsString := os.Getenv("ALLOWED_IPS")
34+
// 将字符串以逗号分隔为切片
2235
// 定义允许连接的 IP 地址
23-
allowedIPs := []string{"127.0.0.1", "183.238.224.118", "110.40.194.189"}
36+
allowedIPs := strings.Split(allowedIPsString, ",")
37+
log.Printf("allowedIPs: %v", allowedIPs)
2438

25-
server, err := net.Listen("tcp", "0.0.0.0:1080")
39+
server, err := net.Listen("tcp", "0.0.0.0:"+os.Getenv("PORT"))
2640
if err != nil {
2741
panic(err)
2842
}
2943
defer server.Close()
30-
fmt.Println("Server started. Listening on :1080")
44+
log.Println("Server started. Listening on :" + os.Getenv("PORT"))
3145

3246
for {
3347
conn, err := server.Accept()
@@ -48,13 +62,13 @@ func main() {
4862

4963
// 如果客户端 IP 不在白名单中,关闭连接
5064
if !allowed {
51-
fmt.Println("Connection from", clientIP, "is not allowed")
65+
// log.Println("Connection from", clientIP, "is not allowed")
5266
conn.Close()
5367
continue
5468
}
5569

5670
// 处理客户端连接
57-
fmt.Println("Connection from", clientIP, "is allowed")
71+
log.Println("Connection from", clientIP, "is allowed")
5872
go process(conn)
5973
}
6074
}

0 commit comments

Comments
 (0)