Skip to content

Commit

Permalink
Allow to set HTTP AUTH user and password for signal-cli JSON-RPC API
Browse files Browse the repository at this point in the history
  • Loading branch information
doobry-systemli committed May 19, 2024
1 parent 21d11f1 commit abc34d5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
2 changes: 2 additions & 0 deletions config.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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")
}
Expand Down
23 changes: 16 additions & 7 deletions internal/signal/signal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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)

}
}

0 comments on commit abc34d5

Please sign in to comment.