summaryrefslogtreecommitdiff
path: root/deprecated-webircgateway/pkg/webircgateway/utils.go
blob: 1fc687a27431adc986a3145bacfa60ed54ae633b (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
package webircgateway

import (
	"context"
	"fmt"
	"net"
	"strings"
	"unicode/utf8"

	"golang.org/x/net/html/charset"
	"golang.org/x/time/rate"
)

var privateIPBlocks []*net.IPNet

func init() {
	for _, cidr := range []string{
		"127.0.0.0/8",    // IPv4 loopback
		"10.0.0.0/8",     // RFC1918
		"172.16.0.0/12",  // RFC1918
		"192.168.0.0/16", // RFC1918
		"::1/128",        // IPv6 loopback
		"fe80::/10",      // IPv6 link-local
	} {
		_, block, _ := net.ParseCIDR(cidr)
		privateIPBlocks = append(privateIPBlocks, block)
	}
}

func isPrivateIP(ip net.IP) bool {
	for _, block := range privateIPBlocks {
		if block.Contains(ip) {
			return true
		}
	}
	return false
}

// Username / realname / webirc hostname can all have configurable replacements
func makeClientReplacements(format string, client *Client) string {
	ret := format
	ret = strings.Replace(ret, "%a", client.RemoteAddr, -1)
	ret = strings.Replace(ret, "%i", Ipv4ToHex(client.RemoteAddr), -1)
	ret = strings.Replace(ret, "%h", client.RemoteHostname, -1)
	ret = strings.Replace(ret, "%n", client.IrcState.Nick, -1)
	return ret
}

func Ipv4ToHex(ip string) string {
	var ipParts [4]int
	fmt.Sscanf(ip, "%d.%d.%d.%d", &ipParts[0], &ipParts[1], &ipParts[2], &ipParts[3])
	ipHex := fmt.Sprintf("%02x%02x%02x%02x", ipParts[0], ipParts[1], ipParts[2], ipParts[3])
	return ipHex
}

func ensureUtf8(s string, fromEncoding string) string {
	if utf8.ValidString(s) {
		return s
	}

	encoding, encErr := charset.Lookup(fromEncoding)
	if encoding == nil {
		println("encErr:", encErr)
		return ""
	}

	d := encoding.NewDecoder()
	s2, _ := d.String(s)
	return s2
}

func utf8ToOther(s string, toEncoding string) string {
	if toEncoding == "UTF-8" && utf8.ValidString(s) {
		return s
	}

	encoding, _ := charset.Lookup(toEncoding)
	if encoding == nil {
		return ""
	}

	e := encoding.NewEncoder()
	s2, _ := e.String(s)
	return s2
}

func containsOneOf(s string, substrs []string) bool {
	for _, substr := range substrs {
		if strings.Contains(s, substr) {
			return true
		}
	}

	return false
}

func stringInSlice(s string, slice []string) bool {
	for _, v := range slice {
		if v == s {
			return true
		}
	}
	return false
}

func stringInSliceOrDefault(s, def string, validStrings []string) string {
	if stringInSlice(s, validStrings) {
		return s
	}
	return def
}

type ThrottledStringChannel struct {
	in     chan string
	Input  chan<- string
	out    chan string
	Output <-chan string
	*rate.Limiter
}

func NewThrottledStringChannel(wrappedChan chan string, limiter *rate.Limiter) *ThrottledStringChannel {
	out := make(chan string, 50)

	c := &ThrottledStringChannel{
		in:      wrappedChan,
		Input:   wrappedChan,
		out:     out,
		Output:  out,
		Limiter: limiter,
	}

	go c.run()

	return c
}

func (c *ThrottledStringChannel) run() {
	for msg := range c.in {
		// start := time.Now()
		c.Wait(context.Background())
		c.out <- msg
		// elapsed := time.Since(start)
		// fmt.Printf("waited %v to send %v\n", elapsed, msg)
	}

	close(c.out)
}