serverless-ircd 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yml +96 -2
- package/.github/workflows/deploy-aws.yml +129 -0
- package/.github/workflows/deploy-cf.yml +0 -2
- package/.gitmodules +3 -0
- package/AGENTS.md +5 -0
- package/CHANGELOG.md +352 -0
- package/MOTD.txt +3 -0
- package/PLAN-FIXES.md +358 -0
- package/PLAN.md +420 -0
- package/README.md +115 -81
- package/apps/aws-stack/README.md +73 -0
- package/apps/aws-stack/bin/aws.ts +49 -0
- package/apps/aws-stack/cdk.json +10 -0
- package/apps/aws-stack/package.json +41 -0
- package/apps/aws-stack/scripts/smoke-helpers.d.mts +22 -0
- package/apps/aws-stack/scripts/smoke-helpers.mjs +89 -0
- package/apps/aws-stack/scripts/smoke.mjs +142 -0
- package/apps/aws-stack/src/aws-stack.ts +263 -0
- package/apps/aws-stack/src/tables.ts +10 -0
- package/apps/aws-stack/tests/localstack.test.ts +46 -0
- package/apps/aws-stack/tests/smoke-helpers.test.ts +98 -0
- package/apps/aws-stack/tests/stack.test.ts +464 -0
- package/apps/aws-stack/tsconfig.build.json +11 -0
- package/apps/aws-stack/tsconfig.test.json +8 -0
- package/apps/aws-stack/vitest.config.ts +20 -0
- package/apps/cf-worker/package.json +1 -1
- package/apps/cf-worker/wrangler.toml +25 -0
- package/apps/local-cli/package.json +1 -1
- package/apps/local-cli/src/config-loader.ts +56 -0
- package/apps/local-cli/src/server.ts +241 -32
- package/apps/local-cli/tests/config-loader.test.ts +107 -0
- package/apps/local-cli/tests/e2e.test.ts +74 -0
- package/apps/local-cli/tests/security-e2e.test.ts +239 -0
- package/biome.json +3 -1
- package/dashboards/cloudwatch-irc.json +139 -0
- package/docs/AWS-Adapter-Architecture.md +494 -0
- package/docs/AWS-Deployment.md +1107 -0
- package/docs/Cloudflare-Deployment-Guide.md +660 -0
- package/docs/Home.md +1 -0
- package/docs/Observability.md +87 -0
- package/docs/PlanIRCv3Websocket.md +489 -0
- package/docs/PlanWebClient.md +451 -0
- package/docs/Release-Process.md +440 -0
- package/package.json +8 -2
- package/packages/aws-adapter/README.md +35 -0
- package/packages/aws-adapter/package.json +52 -0
- package/packages/aws-adapter/src/aws-runtime.ts +783 -0
- package/packages/aws-adapter/src/cdk-table-defs.ts +69 -0
- package/packages/aws-adapter/src/config-loader.ts +96 -0
- package/packages/aws-adapter/src/dynamo.ts +61 -0
- package/packages/aws-adapter/src/handlers/connect.ts +44 -0
- package/packages/aws-adapter/src/handlers/default.ts +322 -0
- package/packages/aws-adapter/src/handlers/disconnect.ts +29 -0
- package/packages/aws-adapter/src/handlers/index.ts +248 -0
- package/packages/aws-adapter/src/handlers/ping-checker.ts +217 -0
- package/packages/aws-adapter/src/handlers/state.ts +13 -0
- package/packages/aws-adapter/src/handlers/sweeper.ts +84 -0
- package/packages/aws-adapter/src/index.ts +67 -0
- package/packages/aws-adapter/src/message-store.ts +34 -0
- package/packages/aws-adapter/src/serialize.ts +283 -0
- package/packages/aws-adapter/src/tables.ts +53 -0
- package/packages/aws-adapter/tests/aws-harness.ts +408 -0
- package/packages/aws-adapter/tests/aws-integration.test.ts +17 -0
- package/packages/aws-adapter/tests/aws-runtime.test.ts +654 -0
- package/packages/aws-adapter/tests/config-loader.test.ts +158 -0
- package/packages/aws-adapter/tests/dynamo.test.ts +57 -0
- package/packages/aws-adapter/tests/global-setup.ts +301 -0
- package/packages/aws-adapter/tests/gone-exception.test.ts +271 -0
- package/packages/aws-adapter/tests/handlers.test.ts +427 -0
- package/packages/aws-adapter/tests/message-store.test.ts +55 -0
- package/packages/aws-adapter/tests/ping-checker.test.ts +571 -0
- package/packages/aws-adapter/tests/serialize.test.ts +249 -0
- package/packages/aws-adapter/tests/smoke.test.ts +48 -0
- package/packages/aws-adapter/tests/sweeper.test.ts +420 -0
- package/packages/aws-adapter/tests/tables.test.ts +74 -0
- package/packages/aws-adapter/tests/transactions.test.ts +446 -0
- package/packages/aws-adapter/tsconfig.build.json +16 -0
- package/packages/aws-adapter/tsconfig.test.json +14 -0
- package/packages/aws-adapter/vitest.config.ts +23 -0
- package/packages/cf-adapter/package.json +2 -1
- package/packages/cf-adapter/src/config-loader.ts +106 -0
- package/packages/cf-adapter/src/connection-do.ts +34 -0
- package/packages/cf-adapter/tests/cf-harness.ts +2 -2
- package/packages/cf-adapter/tests/cf-integration.test.ts +2 -2
- package/packages/cf-adapter/tests/config-loader.test.ts +149 -0
- package/packages/cf-adapter/tests/connection-do.test.ts +42 -0
- package/packages/in-memory-runtime/package.json +1 -1
- package/packages/in-memory-runtime/src/in-memory-runtime.ts +108 -3
- package/packages/in-memory-runtime/src/index.ts +1 -1
- package/packages/in-memory-runtime/tests/in-memory-runtime.test.ts +115 -0
- package/packages/irc-core/package.json +12 -2
- package/packages/irc-core/src/admission.ts +216 -0
- package/packages/irc-core/src/caps/capabilities.ts +1 -0
- package/packages/irc-core/src/cloak.ts +81 -0
- package/packages/irc-core/src/commands/cap.ts +1 -2
- package/packages/irc-core/src/commands/chathistory.ts +305 -0
- package/packages/irc-core/src/commands/index.ts +7 -0
- package/packages/irc-core/src/commands/join.ts +59 -7
- package/packages/irc-core/src/commands/privmsg.ts +24 -0
- package/packages/irc-core/src/commands/registration.ts +71 -14
- package/packages/irc-core/src/commands/sasl.ts +251 -0
- package/packages/irc-core/src/config.ts +247 -0
- package/packages/irc-core/src/flood-control.ts +175 -0
- package/packages/irc-core/src/index.ts +5 -0
- package/packages/irc-core/src/ports.ts +655 -0
- package/packages/irc-core/src/protocol/base64.ts +16 -0
- package/packages/irc-core/src/protocol/index.ts +2 -1
- package/packages/irc-core/src/protocol/numerics.ts +11 -0
- package/packages/irc-core/src/protocol/parser.ts +27 -2
- package/packages/irc-core/src/state/connection.ts +41 -0
- package/packages/irc-core/src/types.ts +66 -2
- package/packages/irc-core/stryker.commands.conf.json +41 -0
- package/packages/irc-core/stryker.protocol.conf.json +26 -0
- package/packages/irc-core/tests/admission.test.ts +229 -0
- package/packages/irc-core/tests/caps/capabilities.test.ts +22 -0
- package/packages/irc-core/tests/cloak.test.ts +78 -0
- package/packages/irc-core/tests/commands/chathistory.test.ts +996 -0
- package/packages/irc-core/tests/commands/join.test.ts +246 -2
- package/packages/irc-core/tests/commands/privmsg.test.ts +146 -2
- package/packages/irc-core/tests/commands/registration.test.ts +277 -9
- package/packages/irc-core/tests/commands/sasl.test.ts +536 -0
- package/packages/irc-core/tests/config.test.ts +646 -0
- package/packages/irc-core/tests/flood-control.test.ts +394 -0
- package/packages/irc-core/tests/message-store.test.ts +530 -0
- package/packages/irc-core/tests/parser.test.ts +44 -1
- package/packages/irc-core/tests/ports.test.ts +263 -0
- package/packages/irc-server/package.json +1 -1
- package/packages/irc-server/src/actor.ts +162 -44
- package/packages/irc-server/src/dispatch.ts +89 -5
- package/packages/irc-server/src/index.ts +2 -0
- package/packages/irc-server/src/routing.ts +151 -0
- package/packages/irc-server/tests/actor.test.ts +470 -14
- package/packages/irc-server/tests/dispatch.test.ts +84 -0
- package/packages/irc-server/tests/routing.test.ts +201 -0
- package/packages/irc-test-support/README.md +32 -0
- package/packages/irc-test-support/package.json +37 -0
- package/packages/{cf-adapter/tests/integration → irc-test-support/src}/in-memory-harness.ts +1 -1
- package/packages/irc-test-support/src/index.ts +24 -0
- package/packages/{cf-adapter/tests/integration/harness.test.ts → irc-test-support/tests/in-memory-harness.test.ts} +1 -1
- package/packages/{cf-adapter/tests/integration/scenarios.test.ts → irc-test-support/tests/in-memory-scenarios.test.ts} +1 -2
- package/packages/irc-test-support/tests/smoke.test.ts +11 -0
- package/packages/irc-test-support/tsconfig.build.json +16 -0
- package/packages/irc-test-support/tsconfig.test.json +14 -0
- package/packages/irc-test-support/vitest.config.ts +20 -0
- package/progress.md +107 -0
- package/tickets.md +2485 -0
- package/tools/ci-hardening/package.json +30 -0
- package/tools/ci-hardening/src/index.ts +6 -0
- package/tools/ci-hardening/src/validate.ts +103 -0
- package/tools/ci-hardening/tests/__no_thresholds__.txt +11 -0
- package/tools/ci-hardening/tests/__partial_thresholds__.txt +16 -0
- package/tools/ci-hardening/tests/__stryker_bad__.json +4 -0
- package/tools/ci-hardening/tests/validate.test.ts +177 -0
- package/tools/ci-hardening/tsconfig.build.json +12 -0
- package/tools/ci-hardening/tsconfig.test.json +10 -0
- package/tools/ci-hardening/vitest.config.ts +21 -0
- package/tools/tcp-ws-forwarder/package.json +1 -1
- package/tools/tcp-ws-forwarder/src/forwarder.ts +34 -23
- package/tools/tcp-ws-forwarder/src/logger.ts +155 -0
- package/tools/tcp-ws-forwarder/src/main.ts +33 -14
- package/tools/tcp-ws-forwarder/tests/forwarder.test.ts +86 -0
- package/tools/tcp-ws-forwarder/tests/logger.test.ts +136 -0
- package/webircgateway/LICENSE +201 -0
- package/webircgateway/Makefile +44 -0
- package/webircgateway/README.md +134 -0
- package/webircgateway/config.conf.example +135 -0
- package/webircgateway/go.mod +16 -0
- package/webircgateway/go.sum +89 -0
- package/webircgateway/main.go +118 -0
- package/webircgateway/pkg/dnsbl/dnsbl.go +121 -0
- package/webircgateway/pkg/identd/identd.go +86 -0
- package/webircgateway/pkg/identd/rpcclient.go +59 -0
- package/webircgateway/pkg/irc/isupport.go +56 -0
- package/webircgateway/pkg/irc/message.go +217 -0
- package/webircgateway/pkg/irc/state.go +79 -0
- package/webircgateway/pkg/proxy/proxy.go +129 -0
- package/webircgateway/pkg/proxy/server.go +237 -0
- package/webircgateway/pkg/recaptcha/recaptcha.go +59 -0
- package/webircgateway/pkg/webircgateway/client.go +741 -0
- package/webircgateway/pkg/webircgateway/client_command_handlers.go +495 -0
- package/webircgateway/pkg/webircgateway/config.go +385 -0
- package/webircgateway/pkg/webircgateway/gateway.go +278 -0
- package/webircgateway/pkg/webircgateway/gateway_utils.go +133 -0
- package/webircgateway/pkg/webircgateway/hooks.go +152 -0
- package/webircgateway/pkg/webircgateway/letsencrypt.go +41 -0
- package/webircgateway/pkg/webircgateway/messagetags.go +103 -0
- package/webircgateway/pkg/webircgateway/transport_kiwiirc.go +206 -0
- package/webircgateway/pkg/webircgateway/transport_sockjs.go +107 -0
- package/webircgateway/pkg/webircgateway/transport_tcp.go +113 -0
- package/webircgateway/pkg/webircgateway/transport_websocket.go +126 -0
- package/webircgateway/pkg/webircgateway/utils.go +147 -0
- package/webircgateway/plugins/example/plugin.go +11 -0
- package/webircgateway/plugins/stats/plugin.go +52 -0
- package/webircgateway/staticcheck.conf +1 -0
- package/webircgateway/webircgateway.svg +3 -0
- package/packages/cf-adapter/tests/integration/index.ts +0 -17
- /package/packages/{cf-adapter/tests/integration → irc-test-support/src}/harness.ts +0 -0
- /package/packages/{cf-adapter/tests/integration → irc-test-support/src}/scenarios.ts +0 -0
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
package webircgateway
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"errors"
|
|
5
|
+
"strconv"
|
|
6
|
+
"strings"
|
|
7
|
+
"time"
|
|
8
|
+
|
|
9
|
+
"github.com/golang-jwt/jwt/v4"
|
|
10
|
+
"github.com/kiwiirc/webircgateway/pkg/irc"
|
|
11
|
+
"github.com/kiwiirc/webircgateway/pkg/recaptcha"
|
|
12
|
+
"golang.org/x/net/html/charset"
|
|
13
|
+
"golang.org/x/time/rate"
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
var MAX_EXTJWT_SIZE = 200
|
|
17
|
+
|
|
18
|
+
/*
|
|
19
|
+
* ProcessLineFromUpstream
|
|
20
|
+
* Processes and makes any changes to a line of data sent from an upstream
|
|
21
|
+
*/
|
|
22
|
+
func (c *Client) ProcessLineFromUpstream(data string) string {
|
|
23
|
+
client := c
|
|
24
|
+
|
|
25
|
+
m, parseErr := irc.ParseLine(data)
|
|
26
|
+
if parseErr != nil {
|
|
27
|
+
return data
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
pLen := len(m.Params)
|
|
31
|
+
|
|
32
|
+
if pLen > 0 && m.Command == "NICK" && m.Prefix.Nick == c.IrcState.Nick {
|
|
33
|
+
client.IrcState.Nick = m.Params[0]
|
|
34
|
+
}
|
|
35
|
+
if pLen > 0 && m.Command == "001" {
|
|
36
|
+
client.IrcState.Nick = m.Params[0]
|
|
37
|
+
client.State = ClientStateConnected
|
|
38
|
+
client.ServerMessagePrefix = *m.Prefix
|
|
39
|
+
|
|
40
|
+
// Throttle writes if configured, but only after registration is complete. Typical IRCd
|
|
41
|
+
// behavior is to not throttle registration commands.
|
|
42
|
+
client.ThrottledRecv.Limiter = rate.NewLimiter(rate.Limit(client.UpstreamConfig.Throttle), 1)
|
|
43
|
+
}
|
|
44
|
+
if pLen > 0 && m.Command == "005" {
|
|
45
|
+
tokenPairs := m.Params[1 : pLen-1]
|
|
46
|
+
iSupport := c.IrcState.ISupport
|
|
47
|
+
iSupport.Received = true
|
|
48
|
+
iSupport.Tags = m.Tags
|
|
49
|
+
iSupport.AddTokens(tokenPairs)
|
|
50
|
+
}
|
|
51
|
+
if c.IrcState.ISupport.Received && !c.IrcState.ISupport.Injected && m.Command != "005" {
|
|
52
|
+
iSupport := c.IrcState.ISupport
|
|
53
|
+
iSupport.Injected = true
|
|
54
|
+
|
|
55
|
+
msg := irc.NewMessage()
|
|
56
|
+
msg.Command = "005"
|
|
57
|
+
msg.Prefix = &c.ServerMessagePrefix
|
|
58
|
+
msg.Params = append(msg.Params, c.IrcState.Nick)
|
|
59
|
+
|
|
60
|
+
if iSupport.HasToken("EXTJWT") {
|
|
61
|
+
c.Log(1, "Upstream already supports EXTJWT, disabling feature")
|
|
62
|
+
c.Features.ExtJwt = false
|
|
63
|
+
} else {
|
|
64
|
+
// Add EXTJWT ISupport token
|
|
65
|
+
msg.Params = append(msg.Params, "EXTJWT=1")
|
|
66
|
+
iSupport.AddToken("EXTJWT=1")
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
msg.Params = append(msg.Params, "are supported by this server")
|
|
70
|
+
if timeTag, ok := c.IrcState.ISupport.Tags["time"]; ok {
|
|
71
|
+
msg.Tags["time"] = timeTag
|
|
72
|
+
}
|
|
73
|
+
if len(msg.Params) > 2 {
|
|
74
|
+
// Extra tokens were added, send the line
|
|
75
|
+
c.SendClientSignal("data", msg.ToLine())
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if pLen > 0 && m.Command == "JOIN" && m.Prefix.Nick == c.IrcState.Nick {
|
|
79
|
+
channel := irc.NewStateChannel(m.GetParam(0, ""))
|
|
80
|
+
c.IrcState.SetChannel(channel)
|
|
81
|
+
}
|
|
82
|
+
if pLen > 0 && m.Command == "PART" && m.Prefix.Nick == c.IrcState.Nick {
|
|
83
|
+
c.IrcState.RemoveChannel(m.GetParam(0, ""))
|
|
84
|
+
}
|
|
85
|
+
if pLen > 0 && m.Command == "QUIT" && m.Prefix.Nick == c.IrcState.Nick {
|
|
86
|
+
c.IrcState.ClearChannels()
|
|
87
|
+
}
|
|
88
|
+
// :server.com 900 m m!m@irc-3jg.1ab.j4ep8h.IP prawnsalad :You are now logged in as prawnsalad
|
|
89
|
+
if pLen > 0 && m.Command == "900" {
|
|
90
|
+
c.IrcState.Account = m.GetParam(2, "")
|
|
91
|
+
}
|
|
92
|
+
// :server.com 901 itsonlybinary itsonlybinary!itsonlybina@user/itsonlybinary :You are now logged out
|
|
93
|
+
if m.Command == "901" {
|
|
94
|
+
c.IrcState.Account = ""
|
|
95
|
+
}
|
|
96
|
+
// :prawnsalad!prawn@kiwiirc/prawnsalad MODE #kiwiirc-dev +oo notprawn kiwi-n75
|
|
97
|
+
if pLen > 0 && m.Command == "MODE" {
|
|
98
|
+
if strings.HasPrefix(m.GetParam(0, ""), "#") {
|
|
99
|
+
channelName := m.GetParam(0, "")
|
|
100
|
+
modes := m.GetParam(1, "")
|
|
101
|
+
|
|
102
|
+
channel := c.IrcState.GetChannel(channelName)
|
|
103
|
+
if channel != nil {
|
|
104
|
+
channel = irc.NewStateChannel(channelName)
|
|
105
|
+
c.IrcState.SetChannel(channel)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
adding := false
|
|
109
|
+
paramIdx := 1
|
|
110
|
+
for i := 0; i < len(modes); i++ {
|
|
111
|
+
mode := string(modes[i])
|
|
112
|
+
|
|
113
|
+
if mode == "+" {
|
|
114
|
+
adding = true
|
|
115
|
+
} else if mode == "-" {
|
|
116
|
+
adding = false
|
|
117
|
+
} else {
|
|
118
|
+
paramIdx++
|
|
119
|
+
param := m.GetParam(paramIdx, "")
|
|
120
|
+
if strings.EqualFold(param, c.IrcState.Nick) {
|
|
121
|
+
if adding {
|
|
122
|
+
channel.Modes[mode] = ""
|
|
123
|
+
} else {
|
|
124
|
+
delete(channel.Modes, mode)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// If upstream reports that it supports message-tags natively, disable the wrapping of this feature for
|
|
133
|
+
// this client
|
|
134
|
+
if pLen >= 3 &&
|
|
135
|
+
strings.ToUpper(m.Command) == "CAP" &&
|
|
136
|
+
m.GetParamU(1, "") == "LS" {
|
|
137
|
+
// The CAPs could be param 2 or 3 depending on if were using multiple lines to list them all.
|
|
138
|
+
caps := ""
|
|
139
|
+
if pLen >= 4 && m.Params[2] == "*" {
|
|
140
|
+
caps = m.GetParamU(3, "")
|
|
141
|
+
} else {
|
|
142
|
+
caps = m.GetParamU(2, "")
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if containsOneOf(caps, []string{"DRAFT/MESSAGE-TAGS-0.2", "MESSAGE-TAGS"}) {
|
|
146
|
+
c.Log(1, "Upstream already supports Messagetags, disabling feature")
|
|
147
|
+
c.Features.Messagetags = false
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Inject message-tags cap into the last line of IRCd capabilities
|
|
151
|
+
if c.Features.Messagetags && m.Params[2] != "*" {
|
|
152
|
+
m.Params[2] += " message-tags"
|
|
153
|
+
data = m.ToLine()
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// If we requested message-tags, make sure to include it in the ACK when
|
|
158
|
+
// the IRCd sends the ACK through
|
|
159
|
+
if m != nil &&
|
|
160
|
+
client.RequestedMessageTagsCap != "" &&
|
|
161
|
+
strings.ToUpper(m.Command) == "CAP" &&
|
|
162
|
+
m.GetParamU(1, "") == "ACK" &&
|
|
163
|
+
!strings.Contains(m.GetParamU(2, ""), "MESSAGE-TAGS") {
|
|
164
|
+
|
|
165
|
+
m.Params[2] += " " + client.RequestedMessageTagsCap
|
|
166
|
+
data = m.ToLine()
|
|
167
|
+
|
|
168
|
+
client.RequestedMessageTagsCap = ""
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if m != nil && client.Features.Messagetags && c.Gateway.messageTags.CanMessageContainClientTags(m) {
|
|
172
|
+
// If we have any message tags stored for this message from a previous PRIVMSG sent
|
|
173
|
+
// by a client, add them back in
|
|
174
|
+
mTags, mTagsExists := c.Gateway.messageTags.GetTagsFromMessage(client, m.Prefix.Nick, m)
|
|
175
|
+
if mTagsExists {
|
|
176
|
+
for k, v := range mTags.Tags {
|
|
177
|
+
m.Tags[k] = v
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
data = m.ToLine()
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return data
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/*
|
|
188
|
+
* ProcessLineFromClient
|
|
189
|
+
* Processes and makes any changes to a line of data sent from a client
|
|
190
|
+
*/
|
|
191
|
+
func (c *Client) ProcessLineFromClient(line string) (string, error) {
|
|
192
|
+
message, err := irc.ParseLine(line)
|
|
193
|
+
// Just pass any random data upstream
|
|
194
|
+
if err != nil {
|
|
195
|
+
return line, nil
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
maybeConnectUpstream := func() {
|
|
199
|
+
verified := false
|
|
200
|
+
if c.RequiresVerification && !c.Verified {
|
|
201
|
+
verified = false
|
|
202
|
+
} else {
|
|
203
|
+
verified = true
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if !c.UpstreamStarted && c.IrcState.Username != "" && c.IrcState.Nick != "" && verified {
|
|
207
|
+
c.connectUpstream()
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if !c.Verified && strings.ToUpper(message.Command) == "CAPTCHA" {
|
|
212
|
+
verified := false
|
|
213
|
+
if len(message.Params) >= 1 {
|
|
214
|
+
captcha := recaptcha.R{
|
|
215
|
+
URL: c.Gateway.Config.ReCaptchaURL,
|
|
216
|
+
Secret: c.Gateway.Config.ReCaptchaSecret,
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
verified = captcha.VerifyResponse(message.Params[0])
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if !verified {
|
|
223
|
+
c.SendIrcError("Invalid captcha")
|
|
224
|
+
c.SendClientSignal("state", "closed", "bad_captcha")
|
|
225
|
+
c.StartShutdown("unverifed")
|
|
226
|
+
} else {
|
|
227
|
+
c.Verified = true
|
|
228
|
+
maybeConnectUpstream()
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return "", nil
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// NICK <nickname>
|
|
235
|
+
if strings.ToUpper(message.Command) == "NICK" && !c.UpstreamStarted {
|
|
236
|
+
if len(message.Params) > 0 {
|
|
237
|
+
c.IrcState.Nick = message.Params[0]
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if !c.UpstreamStarted {
|
|
241
|
+
maybeConnectUpstream()
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// USER <username> <hostname> <servername> <realname>
|
|
246
|
+
if strings.ToUpper(message.Command) == "USER" && !c.UpstreamStarted {
|
|
247
|
+
if len(message.Params) < 4 {
|
|
248
|
+
return line, errors.New("Invalid USER line")
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if c.Gateway.Config.ClientUsername != "" {
|
|
252
|
+
message.Params[0] = makeClientReplacements(c.Gateway.Config.ClientUsername, c)
|
|
253
|
+
}
|
|
254
|
+
if c.Gateway.Config.ClientRealname != "" {
|
|
255
|
+
message.Params[3] = makeClientReplacements(c.Gateway.Config.ClientRealname, c)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
line = message.ToLine()
|
|
259
|
+
|
|
260
|
+
c.IrcState.Username = message.Params[0]
|
|
261
|
+
c.IrcState.RealName = message.Params[3]
|
|
262
|
+
|
|
263
|
+
maybeConnectUpstream()
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if strings.ToUpper(message.Command) == "ENCODING" {
|
|
267
|
+
if len(message.Params) > 0 {
|
|
268
|
+
encoding, _ := charset.Lookup(message.Params[0])
|
|
269
|
+
if encoding == nil {
|
|
270
|
+
c.Log(1, "Requested unknown encoding, %s", message.Params[0])
|
|
271
|
+
} else {
|
|
272
|
+
c.Encoding = message.Params[0]
|
|
273
|
+
c.Log(1, "Set encoding to %s", message.Params[0])
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Don't send the ENCODING command upstream
|
|
278
|
+
return "", nil
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if strings.ToUpper(message.Command) == "HOST" && !c.UpstreamStarted {
|
|
282
|
+
// HOST irc.network.net:6667
|
|
283
|
+
// HOST irc.network.net:+6667
|
|
284
|
+
|
|
285
|
+
if !c.Gateway.Config.Gateway {
|
|
286
|
+
return "", nil
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if len(message.Params) == 0 {
|
|
290
|
+
return "", nil
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
addr := message.Params[0]
|
|
294
|
+
if addr == "" {
|
|
295
|
+
c.SendIrcError("Missing host")
|
|
296
|
+
c.StartShutdown("missing_host")
|
|
297
|
+
return "", nil
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// Parse host:+port into the c.dest* vars
|
|
301
|
+
portSep := strings.LastIndex(addr, ":")
|
|
302
|
+
if portSep == -1 {
|
|
303
|
+
c.DestHost = addr
|
|
304
|
+
c.DestPort = 6667
|
|
305
|
+
c.DestTLS = false
|
|
306
|
+
} else {
|
|
307
|
+
c.DestHost = addr[0:portSep]
|
|
308
|
+
portParam := addr[portSep+1:]
|
|
309
|
+
if len(portParam) > 0 && portParam[0:1] == "+" {
|
|
310
|
+
c.DestTLS = true
|
|
311
|
+
c.DestPort, err = strconv.Atoi(portParam[1:])
|
|
312
|
+
if err != nil {
|
|
313
|
+
c.DestPort = 6697
|
|
314
|
+
}
|
|
315
|
+
} else {
|
|
316
|
+
c.DestPort, err = strconv.Atoi(portParam[0:])
|
|
317
|
+
if err != nil {
|
|
318
|
+
c.DestPort = 6667
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Don't send the HOST command upstream
|
|
324
|
+
return "", nil
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// If the client supports CAP, assume the client also supports parsing MessageTags
|
|
328
|
+
// When upstream replies with its CAP listing, we check if message-tags is supported by the IRCd already and if so,
|
|
329
|
+
// we disable this feature flag again to use the IRCds native support.
|
|
330
|
+
if strings.ToUpper(message.Command) == "CAP" && len(message.Params) > 0 && strings.ToUpper(message.Params[0]) == "LS" {
|
|
331
|
+
c.Log(1, "Enabling client Messagetags feature")
|
|
332
|
+
c.Features.Messagetags = true
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// If we are wrapping the Messagetags feature, make sure the clients REQ message-tags doesn't
|
|
336
|
+
// get sent upstream
|
|
337
|
+
if c.Features.Messagetags && strings.ToUpper(message.Command) == "CAP" && message.GetParamU(0, "") == "REQ" {
|
|
338
|
+
reqCaps := strings.ToLower(message.GetParam(1, ""))
|
|
339
|
+
capsThatEnableMessageTags := []string{"message-tags", "account-tag", "server-time", "batch"}
|
|
340
|
+
|
|
341
|
+
if strings.Contains(reqCaps, "message-tags") {
|
|
342
|
+
// Rebuild the list of requested caps, without message-tags
|
|
343
|
+
caps := strings.Split(reqCaps, " ")
|
|
344
|
+
newCaps := []string{}
|
|
345
|
+
for _, cap := range caps {
|
|
346
|
+
if !strings.Contains(strings.ToLower(cap), "message-tags") {
|
|
347
|
+
newCaps = append(newCaps, cap)
|
|
348
|
+
} else {
|
|
349
|
+
c.RequestedMessageTagsCap = cap
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if len(newCaps) == 0 {
|
|
354
|
+
// The only requested CAP was our emulated message-tags
|
|
355
|
+
// the server will not be sending an ACK so we need to send our own
|
|
356
|
+
c.SendClientSignal("data", "CAP * ACK :"+c.RequestedMessageTagsCap)
|
|
357
|
+
return "", nil
|
|
358
|
+
}
|
|
359
|
+
message.Params[1] = strings.Join(newCaps, " ")
|
|
360
|
+
line = message.ToLine()
|
|
361
|
+
} else if !containsOneOf(reqCaps, capsThatEnableMessageTags) {
|
|
362
|
+
// Didn't request anything that needs message-tags cap so disable it
|
|
363
|
+
c.Features.Messagetags = false
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if c.Features.Messagetags && message.Command == "TAGMSG" {
|
|
368
|
+
if len(message.Params) == 0 {
|
|
369
|
+
return "", nil
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// We can't be 100% sure what this users correct mask is, so just send the nick
|
|
373
|
+
message.Prefix.Nick = c.IrcState.Nick
|
|
374
|
+
message.Prefix.Hostname = ""
|
|
375
|
+
message.Prefix.Username = ""
|
|
376
|
+
|
|
377
|
+
thisHost := strings.ToLower(c.UpstreamConfig.Hostname)
|
|
378
|
+
target := message.Params[0]
|
|
379
|
+
for val := range c.Gateway.Clients.IterBuffered() {
|
|
380
|
+
curClient := val.Val.(*Client)
|
|
381
|
+
sameHost := strings.ToLower(curClient.UpstreamConfig.Hostname) == thisHost
|
|
382
|
+
if !sameHost {
|
|
383
|
+
continue
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// Only send the message on to either the target nick, or the clients in a set channel
|
|
387
|
+
if !strings.EqualFold(target, curClient.IrcState.Nick) && !curClient.IrcState.HasChannel(target) {
|
|
388
|
+
continue
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
curClient.SendClientSignal("data", message.ToLine())
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
return "", nil
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// Check for any client message tags so that we can store them for replaying to other clients
|
|
398
|
+
if c.Features.Messagetags && c.Gateway.messageTags.CanMessageContainClientTags(message) {
|
|
399
|
+
c.Gateway.messageTags.AddTagsFromMessage(c, c.IrcState.Nick, message)
|
|
400
|
+
// Prevent any client tags heading upstream
|
|
401
|
+
for k := range message.Tags {
|
|
402
|
+
if len(k) > 0 && k[0] == '+' {
|
|
403
|
+
delete(message.Tags, k)
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
line = message.ToLine()
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
if c.Features.ExtJwt && strings.ToUpper(message.Command) == "EXTJWT" {
|
|
411
|
+
tokenTarget := message.GetParam(0, "")
|
|
412
|
+
tokenService := message.GetParam(1, "")
|
|
413
|
+
|
|
414
|
+
tokenM := irc.Message{}
|
|
415
|
+
tokenM.Command = "EXTJWT"
|
|
416
|
+
tokenM.Prefix = &c.ServerMessagePrefix
|
|
417
|
+
tokenData := jwt.MapClaims{
|
|
418
|
+
"exp": time.Now().UTC().Add(1 * time.Minute).Unix(),
|
|
419
|
+
"iss": c.UpstreamConfig.Hostname,
|
|
420
|
+
"sub": c.IrcState.Nick,
|
|
421
|
+
"account": c.IrcState.Account,
|
|
422
|
+
"umodes": []string{},
|
|
423
|
+
|
|
424
|
+
// Channel specific claims
|
|
425
|
+
"channel": "",
|
|
426
|
+
"joined": 0,
|
|
427
|
+
"cmodes": []string{},
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// Use the NetworkCommonAddress if a plugin as assigned one.
|
|
431
|
+
// This allows plugins to associate different upstream hosts to the same network
|
|
432
|
+
if c.UpstreamConfig.NetworkCommonAddress != "" {
|
|
433
|
+
tokenData["iss"] = c.UpstreamConfig.NetworkCommonAddress
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
if tokenTarget == "" || tokenTarget == "*" {
|
|
437
|
+
tokenM.Params = append(tokenM.Params, "*")
|
|
438
|
+
} else {
|
|
439
|
+
targetChan := c.IrcState.GetChannel(tokenTarget)
|
|
440
|
+
if targetChan == nil {
|
|
441
|
+
// Channel does not exist in IRC State, send so such channel message
|
|
442
|
+
failMessage := irc.Message{
|
|
443
|
+
Command: "403", // ERR_NOSUCHCHANNEL
|
|
444
|
+
Prefix: &c.ServerMessagePrefix,
|
|
445
|
+
Params: []string{c.IrcState.Nick, tokenTarget, "No such channel"},
|
|
446
|
+
}
|
|
447
|
+
c.SendClientSignal("data", failMessage.ToLine())
|
|
448
|
+
return "", nil
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
tokenM.Params = append(tokenM.Params, tokenTarget)
|
|
452
|
+
|
|
453
|
+
tokenData["channel"] = targetChan.Name
|
|
454
|
+
tokenData["joined"] = targetChan.Joined.Unix()
|
|
455
|
+
|
|
456
|
+
modes := []string{}
|
|
457
|
+
for mode := range targetChan.Modes {
|
|
458
|
+
modes = append(modes, mode)
|
|
459
|
+
}
|
|
460
|
+
tokenData["cmodes"] = modes
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
if tokenService == "" || tokenService == "*" {
|
|
464
|
+
tokenM.Params = append(tokenM.Params, "*")
|
|
465
|
+
} else {
|
|
466
|
+
c.SendIrcFail("EXTJWT", "NO_SUCH_SERVICE", "No such service")
|
|
467
|
+
return "", nil
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
token := jwt.NewWithClaims(jwt.SigningMethodHS256, tokenData)
|
|
471
|
+
tokenSigned, tokenSignedErr := token.SignedString([]byte(c.Gateway.Config.Secret))
|
|
472
|
+
if tokenSignedErr != nil {
|
|
473
|
+
c.Log(3, "Error creating JWT token. %s", tokenSignedErr.Error())
|
|
474
|
+
c.SendIrcFail("EXTJWT", "UNKNOWN_ERROR", "Failed to generate token")
|
|
475
|
+
return "", nil
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
// Spit token if it exceeds max length
|
|
479
|
+
for len(tokenSigned) > MAX_EXTJWT_SIZE {
|
|
480
|
+
tokenSignedPart := tokenSigned[:MAX_EXTJWT_SIZE]
|
|
481
|
+
tokenSigned = tokenSigned[MAX_EXTJWT_SIZE:]
|
|
482
|
+
|
|
483
|
+
tokenPartM := tokenM
|
|
484
|
+
tokenPartM.Params = append(tokenPartM.Params, "*", tokenSignedPart)
|
|
485
|
+
c.SendClientSignal("data", tokenPartM.ToLine())
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
tokenM.Params = append(tokenM.Params, tokenSigned)
|
|
489
|
+
c.SendClientSignal("data", tokenM.ToLine())
|
|
490
|
+
|
|
491
|
+
return "", nil
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
return line, nil
|
|
495
|
+
}
|