-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrolld.go
91 lines (76 loc) · 1.42 KB
/
trolld.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
package main
import (
"fmt"
"log"
"net"
"sync"
"time"
)
// Counter for locking and limits
type Counter struct {
sync.Mutex
count int
Max int
}
// Constructor for locking counters
func NewCounter(max int) Counter {
return Counter{Max: max}
}
// Increment locking counters
func (c Counter) Add() error {
c.Lock()
defer c.Unlock()
if c.count < c.Max {
c.count++
return nil
}
return fmt.Errorf("Connection Limit Exceeded")
}
// Decrement locking counters
func (c Counter) Remove() {
c.Lock()
defer c.Unlock()
c.count = c.count - 1
}
// Trollin' Asset Definition
type Asset struct {
asset []byte
name string
protocol string
}
type Assets struct {
trolls []Asset
}
func (s *Assets) AddAsset(troll Asset) {
s.trolls = append(s.trolls, troll)
}
func (s *Assets) Len() int {
return len(s.trolls)
}
func main() {
// Load trollin' Assets
var a Assets
LoadTrolls(&a, "./trolls")
// Set connection limit
conn_limit := NewCounter(16)
// Listen for connections
l, err := net.Listen("tcp", ":4000")
fmt.Println("Waiting for connections on :4000")
if err != nil {
log.Fatal(err)
}
defer l.Close()
for {
// Check Connection Limit
if e := conn_limit.Add(); e != nil {
time.Sleep(1)
}
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
// Spawning handler for Connection
fmt.Println("Spawning go routine for ", conn.RemoteAddr())
go Telnet(conn, a, conn_limit)
}
}