-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
135 lines (121 loc) · 2.79 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"bytes"
"fmt"
"net"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
)
var outDial string
var pid int
func init() {
initProcess()
}
func initProcess() {
var err error
args := os.Args
bg := os.Getenv("NOHUP") == "1"
preferredDial := os.Getenv("TARGET")
outDial, pid, err = checkExistingProcess()
if err == nil {
if isPortListening(outDial) && outDial == preferredDial {
fmt.Printf("Process is already running on %s\n", outDial)
return
} else {
fmt.Printf("Killing stale process %d \n", pid)
processKill(pid)
}
}
// No existing process found or not listening, start a new one
if preferredDial == "" {
outDial, err = getFreeDial()
if err != nil {
panic("Can't get free port")
}
} else {
outDial = preferredDial
}
// Check if there are additional arguments
if len(args) > 1 {
if bg {
var out bytes.Buffer
var stderr bytes.Buffer
cmdd := generateBgCmd(args[1], args[2:]...)
cmd := exec.Command("bash", "-c", cmdd)
cmd.Stdout = &out
cmd.Stderr = &stderr
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
Pgid: 0,
}
cmd.Env = os.Environ()
if strings.HasPrefix(outDial, LOCAL_PREFIX) {
cmd.Env = append(cmd.Env, fmt.Sprintf("PORT=%s", outDial[len(LOCAL_PREFIX):]))
}
err = cmd.Run()
if err != nil {
fmt.Printf("Error starting command: %v\n", err)
os.Exit(1)
}
pid, err = strconv.Atoi(out.String())
if nil != err {
fmt.Printf("Error starting command: %v\n", err)
os.Exit(1)
}
if pid == 0 {
fmt.Printf("invalid PID of 0")
os.Exit(1)
}
writePidPortFile(pid, outDial)
fmt.Printf("Started process %s with PID %d in background\n", args[1], pid)
} else {
cmd := exec.Command(args[1], args[2:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = os.Environ()
if strings.HasPrefix(outDial, LOCAL_PREFIX) {
cmd.Env = append(cmd.Env, fmt.Sprintf("PORT=%s", outDial[len(LOCAL_PREFIX):]))
}
// Start the specified command
err := cmd.Start()
if err != nil {
fmt.Printf("Error starting command: %v\n", err)
os.Exit(1)
}
pid = cmd.Process.Pid
fmt.Printf("Started process %s with PID %d\n", args[1], pid)
}
}
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080" // Default to port 8080 if no PORT env variable is set
}
err := startProxy("0.0.0.0:" + port)
if err != nil {
panic(err)
}
}
func startProxy(address string) error {
proxy := Proxy{
DialTarget: outDial,
}
listener, err := net.Listen("tcp", address)
if err != nil {
return err
}
defer listener.Close()
fmt.Printf("Proxy server listening on %s\n", address)
for {
conn, err := listener.Accept()
if err != nil {
fmt.Printf("Error accepting connection: %v\n", err)
continue
}
go proxy.handleConnection(conn)
}
}