diff --git a/config.yml.dist b/config.yml.dist index 8e23ebc3..1fd6a00b 100644 --- a/config.yml.dist +++ b/config.yml.dist @@ -16,6 +16,8 @@ telegram: # signal group configuration signal_group: api_url: "" + api_user: "signal-cli" + api_pass: "" account: "" # listen port for prometheus metrics exporter metrics_listen: ":8181" diff --git a/internal/config/config.go b/internal/config/config.go index dca4c725..99970f23 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -38,6 +38,8 @@ type Telegram struct { type SignalGroup struct { ApiUrl string `yaml:"api_url"` + ApiUser string `yaml:"api_user"` + ApiPass string `yaml:"api_pass"` Account string } @@ -122,6 +124,12 @@ func LoadConfig(path string) Config { if os.Getenv("TICKER_SIGNAL_GROUP_API_URL") != "" { c.SignalGroup.ApiUrl = os.Getenv("TICKER_SIGNAL_GROUP_API_URL") } + if os.Getenv("TICKER_SIGNAL_GROUP_API_USER") != "" { + c.SignalGroup.ApiUrl = os.Getenv("TICKER_SIGNAL_GROUP_API_USER") + } + if os.Getenv("TICKER_SIGNAL_GROUP_API_PASS") != "" { + c.SignalGroup.ApiUrl = os.Getenv("TICKER_SIGNAL_GROUP_API_PASS") + } if os.Getenv("TICKER_SIGNAL_GROUP_ACCOUNT") != "" { c.SignalGroup.ApiUrl = os.Getenv("TICKER_SIGNAL_GROUP_ACCOUNT") } diff --git a/internal/signal/signal.go b/internal/signal/signal.go index 043bc7a8..11f5b52a 100644 --- a/internal/signal/signal.go +++ b/internal/signal/signal.go @@ -85,7 +85,7 @@ type DeleteParams struct { func CreateOrUpdateGroup(ts *storage.TickerSignalGroup, config config.Config) error { ctx := context.Background() - client := rpcClient(config.SignalGroup.ApiUrl) + client := rpcClient(config) var err error if ts.GroupID == "" { @@ -150,7 +150,7 @@ func CreateOrUpdateGroup(ts *storage.TickerSignalGroup, config config.Config) er func QuitGroup(config config.Config, groupID string) error { ctx := context.Background() - client := rpcClient(config.SignalGroup.ApiUrl) + client := rpcClient(config) params := QuitGroupParams{ Account: config.SignalGroup.Account, @@ -171,7 +171,7 @@ func QuitGroup(config config.Config, groupID string) error { func listGroups(config config.Config) ([]*ListGroupsResponseGroup, error) { ctx := context.Background() - client := rpcClient(config.SignalGroup.ApiUrl) + client := rpcClient(config) params := ListGroupsParams{ Account: config.SignalGroup.Account, @@ -203,7 +203,7 @@ func getGroup(config config.Config, groupID string) (*ListGroupsResponseGroup, e func SendGroupMessage(config config.Config, ss storage.Storage, groupID string, message *storage.Message) error { ctx := context.Background() - client := rpcClient(config.SignalGroup.ApiUrl) + client := rpcClient(config) var attachments []string if len(message.Attachments) > 0 { @@ -250,7 +250,7 @@ func SendGroupMessage(config config.Config, ss storage.Storage, groupID string, func DeleteMessage(config config.Config, groupID string, message *storage.Message) error { ctx := context.Background() - client := rpcClient(config.SignalGroup.ApiUrl) + client := rpcClient(config) params := DeleteParams{ Account: config.SignalGroup.Account, @@ -267,6 +267,15 @@ func DeleteMessage(config config.Config, groupID string, message *storage.Messag return nil } -func rpcClient(apiUrl string) jsonrpc.RPCClient { - return jsonrpc.NewClient(apiUrl) +func rpcClient(config config.Config) jsonrpc.RPCClient { + if config.SignalGroup.ApiUser != "" && config.SignalGroup.ApiPass != "" { + return jsonrpc.NewClientWithOpts(config.SignalGroup.ApiUrl, &jsonrpc.RPCClientOpts{ + CustomHeaders: map[string]string{ + "Authorization": "Basic " + base64.StdEncoding.EncodeToString([]byte(config.SignalGroup.ApiUser+":"+config.SignalGroup.ApiPass)), + }, + }) + } else { + return jsonrpc.NewClient(config.SignalGroup.ApiUrl) + + } }