-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
131 lines (101 loc) · 2.54 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
package main
import (
"flag"
"sync"
"net"
"io"
"log"
"fmt"
"strings"
"github.com/cheikhshift/gos/core"
)
// Data structures to manage
// web server instances
type StaticHost struct {
Lock *sync.RWMutex
Cache map[string]int
}
func NewCache() StaticHost {
return StaticHost{Lock: new(sync.RWMutex), Cache: make(map[string]int)}
}
var (
PortApp,App string
Limit,IpInc int
Count int = 2
LANnet = []string{}
Host StaticHost
bspath = "./launcher.sh"
)
func GetServerAvailable() string {
var index string
Host.Lock.Lock()
defer Host.Lock.Unlock()
for index,concount := range Host.Cache {
if concount < Limit {
Host.Cache[index] += IpInc
return index
}
}
if App != "" {
core.RunCmd(App)
}
lsize := len(LANnet) - IpInc
LANnet[lsize] = fmt.Sprintf("%v:%s",Count, PortApp )
Count++
index = strings.Join(LANnet,".")
//run bash
Host.Cache[index] = IpInc
return index
}
func main() {
lnch := flag.String("app", "", "Run specified terminal command each time a new instance is needed.")
dhcpstr := flag.String("net", "192.168.0.1", "Router LAN IP address (DHCP subnet).")
maxcon := flag.Int("max", 100, "Maximum number of connections per instance. clm-static will divide tasks.")
addby := flag.Int("incby", 1, "Increase last octet of IP when picking the next server.")
dhcpstart := flag.Int("start", 1, "First DHCP assigned IP of your instances (Initial value of last octet in IPv4 address). Example : with value 21, this tool will assume your first instance's ip will be 192.168.0.21")
apport := flag.String("appPort", "8080", "Port your instances will listen on.")
port := flag.String("port", "9000", "Port clm-static should listen on.")
flag.Parse()
App = *lnch
Limit = *maxcon
IpInc = *addby
LANnet = strings.Split(*dhcpstr, ".")
Count = *dhcpstart
Host = NewCache()
PortApp = * apport
ln, err := net.Listen("tcp", fmt.Sprintf(":%s", *port) )
if err != nil {
panic(err)
}
for {
conn, err := ln.Accept()
if err != nil {
log.Println(err)
}
go handleRequest(conn)
}
}
func handleRequest(conn net.Conn) {
ipaddr := GetServerAvailable()
proxy, err := net.Dial("tcp", ipaddr )
if err != nil {
defer handleRequest(conn)
Host.Lock.Lock()
defer Host.Lock.Unlock()
delete(Host.Cache, ipaddr)
return
}
go copyIO(conn, proxy,"")
go copyIO(proxy, conn,ipaddr)
}
func copyIO(src, dest net.Conn, index string) {
defer src.Close()
defer dest.Close()
io.Copy(src, dest)
if index != "" {
Host.Lock.Lock()
defer Host.Lock.Unlock()
Host.Cache[index] -= IpInc
// fmt.Println(Host)
}
}