From 735fab7c20046b4b01445eb724c9e5eb76cebc30 Mon Sep 17 00:00:00 2001 From: Shreyas Ajjarapu Date: Fri, 8 Apr 2022 23:03:13 -0500 Subject: [PATCH 01/10] maybe works? unlikly --- config/bridge.go | 16 +++++++++++++++- example-config.yaml | 3 +++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/config/bridge.go b/config/bridge.go index b5c4620c..de0bf1e3 100644 --- a/config/bridge.go +++ b/config/bridge.go @@ -18,6 +18,7 @@ package config import ( "bytes" + "regexp" "strconv" "strings" "text/template" @@ -141,10 +142,12 @@ func (bc BridgeConfig) FormatUsername(username string) string { type RelayConfig struct { Enabled bool `yaml:"enabled"` Whitelist []string `yaml:"whitelist"` + Blacklist []string `yaml:"blacklist"` MessageFormats map[event.MessageType]string `yaml:"message_formats"` messageTemplates *template.Template `yaml:"-"` whitelistMap map[string]struct{} `yaml:"-"` + blacklistMap map[string]struct{} `yaml:"-"` isAllWhitelisted bool `yaml:"-"` } @@ -176,7 +179,7 @@ func (rc *RelayConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { } func (rc *RelayConfig) IsWhitelisted(userID id.UserID) bool { - if !rc.Enabled { + if !rc.Enabled || rc.IsBlacklist(userID) { return false } else if rc.isAllWhitelisted { return true @@ -189,6 +192,17 @@ func (rc *RelayConfig) IsWhitelisted(userID id.UserID) bool { } } +func (rc *RelayConfig) IsBlacklist(userID id.UserID) bool { + for _, item := range rc.Blacklist { + match, _ := regexp.MatchString(item, userID.String()) + if match { + return true + } + } + return false + +} + type Sender struct { UserID string event.MemberEventContent diff --git a/example-config.yaml b/example-config.yaml index b77e3d7b..87fea925 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -147,6 +147,9 @@ bridge: enabled: false # A list of user IDs and server names who are allowed to be relayed through this bridge. Use * to allow everyone. whitelist: [] + # A list of user IDs and server names who are not allowed to be relayed through this bridge. + # Accepts regex for example to block all users from + blacklist: [] # The formats to use when relaying messages to iMessage. message_formats: m.text: "{{ .Sender.Displayname }}: {{ .Message }}" From 6878661aeb10cb0242f29709dc14f54f0a7d8aec Mon Sep 17 00:00:00 2001 From: Shreyas Ajjarapu Date: Fri, 8 Apr 2022 23:12:12 -0500 Subject: [PATCH 02/10] yea it didnt --- config/bridge.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/bridge.go b/config/bridge.go index de0bf1e3..b0744fd2 100644 --- a/config/bridge.go +++ b/config/bridge.go @@ -179,7 +179,7 @@ func (rc *RelayConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { } func (rc *RelayConfig) IsWhitelisted(userID id.UserID) bool { - if !rc.Enabled || rc.IsBlacklist(userID) { + if !rc.Enabled { return false } else if rc.isAllWhitelisted { return true From 368bed0cf9279042ca506ee6a936096261e45c5c Mon Sep 17 00:00:00 2001 From: Shreyas Ajjarapu Date: Fri, 8 Apr 2022 23:43:23 -0500 Subject: [PATCH 03/10] maybe it did --- config/bridge.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/bridge.go b/config/bridge.go index b0744fd2..dd9b80bf 100644 --- a/config/bridge.go +++ b/config/bridge.go @@ -179,7 +179,7 @@ func (rc *RelayConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { } func (rc *RelayConfig) IsWhitelisted(userID id.UserID) bool { - if !rc.Enabled { + if !rc.Enabled || rc.IsBlacklisted(userID) { return false } else if rc.isAllWhitelisted { return true @@ -192,7 +192,7 @@ func (rc *RelayConfig) IsWhitelisted(userID id.UserID) bool { } } -func (rc *RelayConfig) IsBlacklist(userID id.UserID) bool { +func (rc *RelayConfig) IsBlacklisted(userID id.UserID) bool { for _, item := range rc.Blacklist { match, _ := regexp.MatchString(item, userID.String()) if match { From 2887f2e8ec007e2cfce88cb5b1df80ce7b96f40f Mon Sep 17 00:00:00 2001 From: Shreyas Ajjarapu Date: Sat, 9 Apr 2022 00:12:36 -0500 Subject: [PATCH 04/10] debug why does it never work on the first attempt --- config/bridge.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/config/bridge.go b/config/bridge.go index dd9b80bf..9928441a 100644 --- a/config/bridge.go +++ b/config/bridge.go @@ -23,6 +23,7 @@ import ( "strings" "text/template" + "github.com/prometheus/common/log" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/id" ) @@ -182,6 +183,7 @@ func (rc *RelayConfig) IsWhitelisted(userID id.UserID) bool { if !rc.Enabled || rc.IsBlacklisted(userID) { return false } else if rc.isAllWhitelisted { + return true } else if _, ok := rc.whitelistMap[string(userID)]; ok { return true @@ -194,6 +196,7 @@ func (rc *RelayConfig) IsWhitelisted(userID id.UserID) bool { func (rc *RelayConfig) IsBlacklisted(userID id.UserID) bool { for _, item := range rc.Blacklist { + log.Infoln("Hello: " + item) match, _ := regexp.MatchString(item, userID.String()) if match { return true From ef33ede666b61b9dfed0cb70770f72c08fbec0eb Mon Sep 17 00:00:00 2001 From: Shreyas Ajjarapu Date: Sat, 9 Apr 2022 00:14:28 -0500 Subject: [PATCH 05/10] work on first try --- config/bridge.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/bridge.go b/config/bridge.go index 9928441a..82170d40 100644 --- a/config/bridge.go +++ b/config/bridge.go @@ -18,12 +18,12 @@ package config import ( "bytes" + "log" "regexp" "strconv" "strings" "text/template" - "github.com/prometheus/common/log" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/id" ) @@ -196,7 +196,7 @@ func (rc *RelayConfig) IsWhitelisted(userID id.UserID) bool { func (rc *RelayConfig) IsBlacklisted(userID id.UserID) bool { for _, item := range rc.Blacklist { - log.Infoln("Hello: " + item) + log.Print("Test: " + item) match, _ := regexp.MatchString(item, userID.String()) if match { return true From 8da55adf0f1417c41ad896d336ccf4d8f3c2f32a Mon Sep 17 00:00:00 2001 From: Shreyas Ajjarapu Date: Sat, 9 Apr 2022 02:02:26 -0500 Subject: [PATCH 06/10] debug --- config/bridge.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/bridge.go b/config/bridge.go index 82170d40..5c440141 100644 --- a/config/bridge.go +++ b/config/bridge.go @@ -183,7 +183,6 @@ func (rc *RelayConfig) IsWhitelisted(userID id.UserID) bool { if !rc.Enabled || rc.IsBlacklisted(userID) { return false } else if rc.isAllWhitelisted { - return true } else if _, ok := rc.whitelistMap[string(userID)]; ok { return true @@ -195,6 +194,7 @@ func (rc *RelayConfig) IsWhitelisted(userID id.UserID) bool { } func (rc *RelayConfig) IsBlacklisted(userID id.UserID) bool { + log.Print("Im testing: ") for _, item := range rc.Blacklist { log.Print("Test: " + item) match, _ := regexp.MatchString(item, userID.String()) From 983b8f95ccd1704e9d3ad063ee051465ff4fca55 Mon Sep 17 00:00:00 2001 From: Shreyas Ajjarapu Date: Sat, 9 Apr 2022 14:01:27 -0500 Subject: [PATCH 07/10] debug --- config/bridge.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/config/bridge.go b/config/bridge.go index 5c440141..f2d1c9ba 100644 --- a/config/bridge.go +++ b/config/bridge.go @@ -19,7 +19,6 @@ package config import ( "bytes" "log" - "regexp" "strconv" "strings" "text/template" @@ -168,6 +167,11 @@ func (rc *RelayConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { } } + rc.blacklistMap = make(map[string]struct{}, len(rc.Blacklist)) + for _, item := range rc.Blacklist { + rc.blacklistMap[item] = struct{}{} + } + rc.whitelistMap = make(map[string]struct{}, len(rc.Whitelist)) for _, item := range rc.Whitelist { rc.whitelistMap[item] = struct{}{} @@ -195,15 +199,17 @@ func (rc *RelayConfig) IsWhitelisted(userID id.UserID) bool { func (rc *RelayConfig) IsBlacklisted(userID id.UserID) bool { log.Print("Im testing: ") - for _, item := range rc.Blacklist { - log.Print("Test: " + item) - match, _ := regexp.MatchString(item, userID.String()) - if match { - return true - } + if _, ok := rc.blacklistMap[string(userID)]; ok { + log.Println("Return of user in blacklist: true") + return true + } else { + log.Println("Return of user in blacklist: false") + _, homeserver, _ := userID.Parse() + _, ok = rc.blacklistMap[homeserver] + log.Println("Return of homeserver in blacklist: " + homeserver) + log.Println("Return of homeserver in blacklist boolean: " + strconv.FormatBool(ok)) + return len(homeserver) > 0 && ok } - return false - } type Sender struct { From a23a6d90b5651e1b0101ae96db11df00d4ad2a92 Mon Sep 17 00:00:00 2001 From: Shreyas Ajjarapu Date: Sat, 9 Apr 2022 14:04:09 -0500 Subject: [PATCH 08/10] debug --- config/bridge.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/bridge.go b/config/bridge.go index f2d1c9ba..b3d3d360 100644 --- a/config/bridge.go +++ b/config/bridge.go @@ -170,6 +170,7 @@ func (rc *RelayConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { rc.blacklistMap = make(map[string]struct{}, len(rc.Blacklist)) for _, item := range rc.Blacklist { rc.blacklistMap[item] = struct{}{} + log.Println("Blacklist item: " + item) } rc.whitelistMap = make(map[string]struct{}, len(rc.Whitelist)) @@ -198,7 +199,7 @@ func (rc *RelayConfig) IsWhitelisted(userID id.UserID) bool { } func (rc *RelayConfig) IsBlacklisted(userID id.UserID) bool { - log.Print("Im testing: ") + log.Print("Im testing: " + string(userID)) if _, ok := rc.blacklistMap[string(userID)]; ok { log.Println("Return of user in blacklist: true") return true From 4412ff3b2c7e80308876f89e4ba5403573ea3341 Mon Sep 17 00:00:00 2001 From: Shreyas Ajjarapu Date: Sat, 9 Apr 2022 14:07:39 -0500 Subject: [PATCH 09/10] removed debug --- config/bridge.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/config/bridge.go b/config/bridge.go index b3d3d360..3a32662a 100644 --- a/config/bridge.go +++ b/config/bridge.go @@ -170,7 +170,6 @@ func (rc *RelayConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { rc.blacklistMap = make(map[string]struct{}, len(rc.Blacklist)) for _, item := range rc.Blacklist { rc.blacklistMap[item] = struct{}{} - log.Println("Blacklist item: " + item) } rc.whitelistMap = make(map[string]struct{}, len(rc.Whitelist)) @@ -201,14 +200,10 @@ func (rc *RelayConfig) IsWhitelisted(userID id.UserID) bool { func (rc *RelayConfig) IsBlacklisted(userID id.UserID) bool { log.Print("Im testing: " + string(userID)) if _, ok := rc.blacklistMap[string(userID)]; ok { - log.Println("Return of user in blacklist: true") return true } else { - log.Println("Return of user in blacklist: false") _, homeserver, _ := userID.Parse() _, ok = rc.blacklistMap[homeserver] - log.Println("Return of homeserver in blacklist: " + homeserver) - log.Println("Return of homeserver in blacklist boolean: " + strconv.FormatBool(ok)) return len(homeserver) > 0 && ok } } From 5da7193be6e0b96601ec5d480a32a73e783e4d28 Mon Sep 17 00:00:00 2001 From: Shreyas Ajjarapu Date: Sat, 9 Apr 2022 14:13:12 -0500 Subject: [PATCH 10/10] Updated documentation --- example-config.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/example-config.yaml b/example-config.yaml index 87fea925..f41e7be0 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -148,7 +148,8 @@ bridge: # A list of user IDs and server names who are allowed to be relayed through this bridge. Use * to allow everyone. whitelist: [] # A list of user IDs and server names who are not allowed to be relayed through this bridge. - # Accepts regex for example to block all users from + # Follows the schema above but cannot use * to block everyone (if that what you want just disable relay) + # Blacklist will be checked before whitelist blacklist: [] # The formats to use when relaying messages to iMessage. message_formats: