-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
111 lines (107 loc) · 2.55 KB
/
example_test.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
package plistener_test
import (
"github.com/cevatbarisyilmaz/plistener"
"log"
"net"
"net/http"
"time"
)
func Example() {
tcpAddr := &net.TCPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: 80,
}
//Create the TCP listener
tcpListener, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
log.Fatal(err)
}
//Create a PListener via the TCP listener
pListener := plistener.New(tcpListener)
for {
//Accept the next connection
conn, err := pListener.Accept()
if err != nil {
//Handle the error
log.Fatal(err)
}
//Start a new goroutine to handle the connection
go func() {
//Write hi to the other side and close the connection
_, err := conn.Write([]byte("Hi!"))
if err != nil {
log.Fatal(err)
}
err = conn.Close()
if err != nil {
log.Fatal(err)
}
}()
}
}
func Example_hTTP() {
tcpAddr := &net.TCPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: 80,
}
//Create the TCP listener
tcpListener, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
log.Fatal(err)
}
//Create a PListener via the TCP listener
pListener := plistener.New(tcpListener)
//Initialize a HTTP server
server := &http.Server{}
//Serve HTTP over PListener
err = server.Serve(pListener)
//Handle the error
log.Fatal(err)
}
func Example_advanced() {
tcpAddr := &net.TCPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: 80,
}
//Create the TCP listener
tcpListener, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
log.Fatal(err)
}
//Create a PListener via the TCP listener
pListener := plistener.New(tcpListener)
//Set number of maximum alive connections to 16
pListener.MaxConn = 16
//Set number of maximum alive connections with a single IP to 2
pListener.MaxConnSingleIP = 2
//Let remote IP addresses to create only 2 new connections in a minute and only 20 new connections in an hour
pListener.SetLimiter(plistener.Limiter{
{time.Minute, 2},
{time.Hour, 20},
})
//If an IP exceeds the rate limit or maximum amount of connections limit, ignore all the new requests of it for a day.
pListener.OnSpam = func(ip net.IP) {
pListener.TempBan(ip, time.Now().Add(time.Hour*24))
}
//Give privilege to the localhost to bypass all the limits
pListener.GivePrivilege(net.IPv4(127, 0, 0, 1))
for {
//Wait for a new connection
conn, err := pListener.Accept()
if err != nil {
log.Fatal(err)
}
//Handle the connection
go func() {
//Write hi to the other side and close the connection
_, err := conn.Write([]byte("Hi!"))
if err != nil {
log.Fatal(err)
}
err = conn.Close()
if err != nil {
log.Fatal(err)
}
}()
}
}