forked from urdt/ussd
Compare commits
21 Commits
lash/ssh-4
...
africastal
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
574807d254
|
||
|
|
256ed6491b | ||
|
|
7a02ffcf0c | ||
|
|
dcd8fce59a
|
||
|
|
64a7b49218
|
||
|
|
1bcbb2079e
|
||
|
|
e63468433e
|
||
|
|
9c972ffa6b
|
||
|
|
a11776e1b3
|
||
|
|
6ac9ac29d8
|
||
| 46b2b354fd | |||
|
|
7676cfd40c
|
||
|
|
4e350aa25a
|
||
| 859de0513a | |||
|
|
266d3d06c3
|
||
|
|
92ea3df4aa
|
||
|
|
c46c31ea36
|
||
|
|
da91eed9d4
|
||
|
|
2b865a365b
|
||
|
|
c77558689a
|
||
|
|
a9641fd70d
|
@@ -16,3 +16,6 @@ CREATE_ACCOUNT_URL=http://localhost:5003/api/v2/account/create
|
||||
TRACK_STATUS_URL=https://custodial.sarafu.africa/api/track/
|
||||
BALANCE_URL=https://custodial.sarafu.africa/api/account/status/
|
||||
TRACK_URL=http://localhost:5003/api/v2/account/status
|
||||
|
||||
#AfricasTalking USSD POST endpoint
|
||||
AT_ENDPOINT=/ussd/africastalking
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -6,3 +6,4 @@ go.work*
|
||||
cmd/.state/
|
||||
id_*
|
||||
*.gdbm
|
||||
*.log
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
@@ -25,12 +29,28 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
logg = logging.NewVanilla()
|
||||
scriptDir = path.Join("services", "registration")
|
||||
logg = logging.NewVanilla()
|
||||
scriptDir = path.Join("services", "registration")
|
||||
InfoLogger *log.Logger
|
||||
ErrorLogger *log.Logger
|
||||
)
|
||||
|
||||
func init() {
|
||||
initializers.LoadEnvVariables()
|
||||
|
||||
logFile := "urdt-ussd-africastalking.log"
|
||||
|
||||
file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
InfoLogger = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||
ErrorLogger = log.New(file, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||
|
||||
// Inject into remote package
|
||||
remote.InfoLogger = InfoLogger
|
||||
remote.ErrorLogger = ErrorLogger
|
||||
}
|
||||
|
||||
type atRequestParser struct{}
|
||||
@@ -38,9 +58,30 @@ type atRequestParser struct{}
|
||||
func (arp *atRequestParser) GetSessionId(rq any) (string, error) {
|
||||
rqv, ok := rq.(*http.Request)
|
||||
if !ok {
|
||||
ErrorLogger.Println("got an invalid request:", rq)
|
||||
return "", handlers.ErrInvalidRequest
|
||||
}
|
||||
|
||||
// Capture body (if any) for logging
|
||||
body, err := io.ReadAll(rqv.Body)
|
||||
if err != nil {
|
||||
ErrorLogger.Println("failed to read request body:", err)
|
||||
return "", fmt.Errorf("failed to read request body: %v", err)
|
||||
}
|
||||
// Reset the body for further reading
|
||||
rqv.Body = io.NopCloser(bytes.NewReader(body))
|
||||
|
||||
// Log the body as JSON
|
||||
bodyLog := map[string]string{"body": string(body)}
|
||||
logBytes, err := json.Marshal(bodyLog)
|
||||
if err != nil {
|
||||
ErrorLogger.Println("failed to marshal request body:", err)
|
||||
} else {
|
||||
InfoLogger.Println("Received request:", string(logBytes))
|
||||
}
|
||||
|
||||
if err := rqv.ParseForm(); err != nil {
|
||||
ErrorLogger.Println("failed to parse form data: %v", err)
|
||||
return "", fmt.Errorf("failed to parse form data: %v", err)
|
||||
}
|
||||
|
||||
@@ -156,9 +197,13 @@ func main() {
|
||||
rp := &atRequestParser{}
|
||||
bsh := handlers.NewBaseSessionHandler(cfg, rs, stateStore, userdataStore, rp, hl)
|
||||
sh := httpserver.NewATSessionHandler(bsh)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle(initializers.GetEnv("AT_ENDPOINT", "/"), sh)
|
||||
|
||||
s := &http.Server{
|
||||
Addr: fmt.Sprintf("%s:%s", host, strconv.Itoa(int(port))),
|
||||
Handler: sh,
|
||||
Handler: mux,
|
||||
}
|
||||
s.RegisterOnShutdown(sh.Shutdown)
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
@@ -23,12 +24,27 @@ import (
|
||||
var (
|
||||
logg = logging.NewVanilla()
|
||||
scriptDir = path.Join("services", "registration")
|
||||
InfoLogger *log.Logger
|
||||
ErrorLogger *log.Logger
|
||||
)
|
||||
|
||||
func init() {
|
||||
initializers.LoadEnvVariables()
|
||||
}
|
||||
|
||||
logFile := "urdt-ussd-async.log"
|
||||
|
||||
file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
InfoLogger = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||
ErrorLogger = log.New(file, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||
|
||||
// Inject into remote package
|
||||
remote.InfoLogger = InfoLogger
|
||||
remote.ErrorLogger = ErrorLogger
|
||||
}
|
||||
type asyncRequestParser struct {
|
||||
sessionId string
|
||||
input []byte
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
@@ -26,10 +27,26 @@ import (
|
||||
var (
|
||||
logg = logging.NewVanilla()
|
||||
scriptDir = path.Join("services", "registration")
|
||||
InfoLogger *log.Logger
|
||||
ErrorLogger *log.Logger
|
||||
)
|
||||
|
||||
func init() {
|
||||
initializers.LoadEnvVariables()
|
||||
|
||||
logFile := "urdt-ussd-http.log"
|
||||
|
||||
file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
InfoLogger = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||
ErrorLogger = log.New(file, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||
|
||||
// Inject into remote package
|
||||
remote.InfoLogger = InfoLogger
|
||||
remote.ErrorLogger = ErrorLogger
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
17
cmd/main.go
17
cmd/main.go
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
@@ -20,10 +21,26 @@ import (
|
||||
var (
|
||||
logg = logging.NewVanilla()
|
||||
scriptDir = path.Join("services", "registration")
|
||||
InfoLogger *log.Logger
|
||||
ErrorLogger *log.Logger
|
||||
)
|
||||
|
||||
func init() {
|
||||
initializers.LoadEnvVariables()
|
||||
|
||||
logFile := "urdt-ussd-cli.log"
|
||||
|
||||
file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
InfoLogger = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||
ErrorLogger = log.New(file, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||
|
||||
// Inject into remote package
|
||||
remote.InfoLogger = InfoLogger
|
||||
remote.ErrorLogger = ErrorLogger
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -1254,7 +1254,8 @@ func (h *Handlers) SetDefaultVoucher(ctx context.Context, sym string, input []by
|
||||
// Fetch vouchers from the API using the public key
|
||||
vouchersResp, err := h.accountService.FetchVouchers(ctx, string(publicKey))
|
||||
if err != nil {
|
||||
return res, err
|
||||
res.FlagSet = append(res.FlagSet, flag_no_active_voucher)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// Return if there is no voucher
|
||||
|
||||
@@ -54,7 +54,7 @@
|
||||
},
|
||||
{
|
||||
"input": "1235",
|
||||
"expectedContent": "Incorrect pin\n1:retry\n9:Quit"
|
||||
"expectedContent": "Incorrect pin\n1:Retry\n9:Quit"
|
||||
},
|
||||
{
|
||||
"input": "1",
|
||||
@@ -95,7 +95,7 @@
|
||||
},
|
||||
{
|
||||
"input": "1235",
|
||||
"expectedContent": "Incorrect pin\n1:retry\n9:Quit"
|
||||
"expectedContent": "Incorrect pin\n1:Retry\n9:Quit"
|
||||
},
|
||||
{
|
||||
"input": "1",
|
||||
@@ -141,7 +141,7 @@
|
||||
},
|
||||
{
|
||||
"input": "1235",
|
||||
"expectedContent": "Incorrect pin\n1:retry\n9:Quit"
|
||||
"expectedContent": "Incorrect pin\n1:Retry\n9:Quit"
|
||||
},
|
||||
{
|
||||
"input": "1",
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
},
|
||||
{
|
||||
"input": "1111",
|
||||
"expectedContent": "The PIN is not a match. Try again\n1:retry\n9:Quit"
|
||||
"expectedContent": "The PIN is not a match. Try again\n1:Retry\n9:Quit"
|
||||
},
|
||||
{
|
||||
"input": "1",
|
||||
@@ -65,7 +65,7 @@
|
||||
},
|
||||
{
|
||||
"input": "000",
|
||||
"expectedContent": "000 is not registered or invalid, please try again:\n1:retry\n9:Quit"
|
||||
"expectedContent": "000 is not registered or invalid, please try again:\n1:Retry\n9:Quit"
|
||||
},
|
||||
{
|
||||
"input": "1",
|
||||
@@ -77,7 +77,7 @@
|
||||
},
|
||||
{
|
||||
"input": "10000000",
|
||||
"expectedContent": "Amount 10000000 is invalid, please try again:\n1:retry\n9:Quit"
|
||||
"expectedContent": "Amount 10000000 is invalid, please try again:\n1:Retry\n9:Quit"
|
||||
},
|
||||
{
|
||||
"input": "1",
|
||||
@@ -89,7 +89,7 @@
|
||||
},
|
||||
{
|
||||
"input": "1222",
|
||||
"expectedContent": "Incorrect pin\n1:retry\n9:Quit"
|
||||
"expectedContent": "Incorrect pin\n1:Retry\n9:Quit"
|
||||
},
|
||||
{
|
||||
"input": "1",
|
||||
@@ -140,7 +140,7 @@
|
||||
},
|
||||
{
|
||||
"input": "6",
|
||||
"expectedContent": "Address: {public_key}\n9:Quit"
|
||||
"expectedContent": "Address: {public_key}\n0:Back\n9:Quit"
|
||||
},
|
||||
{
|
||||
"input": "9",
|
||||
|
||||
@@ -1,20 +1,24 @@
|
||||
package remote
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
||||
"git.grassecon.net/urdt/ussd/config"
|
||||
"git.grassecon.net/urdt/ussd/models"
|
||||
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||
)
|
||||
|
||||
var (
|
||||
InfoLogger *log.Logger
|
||||
ErrorLogger *log.Logger
|
||||
)
|
||||
|
||||
type AccountServiceInterface interface {
|
||||
@@ -51,7 +55,7 @@ func (as *AccountService) TrackAccountStatus(ctx context.Context, publicKey stri
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = doCustodialRequest(ctx, req, &r)
|
||||
_, err = doCustodialRequest(ctx, req, &r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -79,7 +83,6 @@ func (as *AccountService) CheckBalance(ctx context.Context, publicKey string) (*
|
||||
return &balanceResult, err
|
||||
}
|
||||
|
||||
|
||||
// CreateAccount creates a new account in the custodial system.
|
||||
// Returns:
|
||||
// - *models.AccountResponse: A pointer to an AccountResponse struct containing the details of the created account.
|
||||
@@ -93,9 +96,9 @@ func (as *AccountService) CreateAccount(ctx context.Context) (*models.AccountRes
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = doCustodialRequest(ctx, req, &r)
|
||||
_, err = doCustodialRequest(ctx, req, &r)
|
||||
if err != nil {
|
||||
log.Printf("Failed to make custodial %s request to endpoint: %s with reason: %s", req.Method, req.URL, err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -118,7 +121,7 @@ func (as *AccountService) FetchVouchers(ctx context.Context, publicKey string) (
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = doDataRequest(ctx, req, r)
|
||||
_, err = doDataRequest(ctx, req, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -126,7 +129,6 @@ func (as *AccountService) FetchVouchers(ctx context.Context, publicKey string) (
|
||||
return r, nil
|
||||
}
|
||||
|
||||
|
||||
// FetchTransactions retrieves the last 10 transactions for a given public key from the data indexer API endpoint
|
||||
// Parameters:
|
||||
// - publicKey: The public key associated with the account.
|
||||
@@ -143,7 +145,7 @@ func (as *AccountService) FetchTransactions(ctx context.Context, publicKey strin
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = doDataRequest(ctx, req, r)
|
||||
_, err = doDataRequest(ctx, req, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -151,7 +153,6 @@ func (as *AccountService) FetchTransactions(ctx context.Context, publicKey strin
|
||||
return r, nil
|
||||
}
|
||||
|
||||
|
||||
// VoucherData retrieves voucher metadata from the data indexer API endpoint.
|
||||
// Parameters:
|
||||
// - address: The voucher address.
|
||||
@@ -173,9 +174,8 @@ func (as *AccountService) VoucherData(ctx context.Context, address string) (*mod
|
||||
}
|
||||
|
||||
func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {
|
||||
var okResponse api.OKResponse
|
||||
var okResponse api.OKResponse
|
||||
var errResponse api.ErrResponse
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
@@ -184,6 +184,7 @@ func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKRespons
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
InfoLogger.Printf("Received response for %s: Status Code: %d | Content-Type: %s", req.URL, resp.StatusCode, resp.Header.Get("Content-Type"))
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -202,7 +203,6 @@ func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKRespons
|
||||
if len(okResponse.Result) == 0 {
|
||||
return nil, errors.New("Empty api result")
|
||||
}
|
||||
return &okResponse, nil
|
||||
|
||||
v, err := json.Marshal(okResponse.Result)
|
||||
if err != nil {
|
||||
@@ -215,10 +215,29 @@ func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKRespons
|
||||
|
||||
func doCustodialRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {
|
||||
req.Header.Set("X-GE-KEY", config.CustodialAPIKey)
|
||||
logRequestDetails(req)
|
||||
return doRequest(ctx, req, rcpt)
|
||||
}
|
||||
|
||||
func doDataRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {
|
||||
req.Header.Set("X-GE-KEY", config.DataAPIKey)
|
||||
logRequestDetails(req)
|
||||
return doRequest(ctx, req, rcpt)
|
||||
}
|
||||
|
||||
func logRequestDetails(req *http.Request) {
|
||||
var bodyBytes []byte
|
||||
contentType := req.Header.Get("Content-Type")
|
||||
if req.Body != nil {
|
||||
bodyBytes, err := io.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
ErrorLogger.Printf("Error reading request body: %s", err)
|
||||
return
|
||||
}
|
||||
req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
|
||||
} else {
|
||||
bodyBytes = []byte("-")
|
||||
}
|
||||
|
||||
InfoLogger.Printf("URL: %s | Content-Type: %s | Method: %s| Request Body: %s", req.URL, contentType, req.Method, string(bodyBytes))
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
LOAD check_identifier 0
|
||||
RELOAD check_identifier
|
||||
MAP check_identifier
|
||||
MOUT back 0
|
||||
MOUT quit 9
|
||||
HALT
|
||||
INCMP _ 0
|
||||
INCMP quit 9
|
||||
|
||||
1
services/registration/address_swa
Normal file
1
services/registration/address_swa
Normal file
@@ -0,0 +1 @@
|
||||
Anwani:{{.check_identifier}}
|
||||
1
services/registration/confirm_others_new_pin_swa
Normal file
1
services/registration/confirm_others_new_pin_swa
Normal file
@@ -0,0 +1 @@
|
||||
Tafadhali thibitisha PIN mpya ya: {{.retrieve_blocked_number}}
|
||||
1
services/registration/enter_other_number_swa
Normal file
1
services/registration/enter_other_number_swa
Normal file
@@ -0,0 +1 @@
|
||||
Weka nambari ya simu ili kutuma ombi la kubadilisha nambari ya siri:
|
||||
1
services/registration/enter_others_new_pin_swa
Normal file
1
services/registration/enter_others_new_pin_swa
Normal file
@@ -0,0 +1 @@
|
||||
Tafadhali weka PIN mpya ya: {{.retrieve_blocked_number}}
|
||||
1
services/registration/no_admin_privilege_swa
Normal file
1
services/registration/no_admin_privilege_swa
Normal file
@@ -0,0 +1 @@
|
||||
Huna mapendeleo ya kufanya kitendo hiki
|
||||
@@ -1 +1 @@
|
||||
You need a voucher to send
|
||||
You need a voucher to proceed
|
||||
@@ -1 +1 @@
|
||||
Unahitaji sarafu kutuma
|
||||
Unahitaji sarafu kuendelea
|
||||
1
services/registration/others_pin_mismatch_swa
Normal file
1
services/registration/others_pin_mismatch_swa
Normal file
@@ -0,0 +1 @@
|
||||
PIN uliyoweka hailingani.Jaribu tena.
|
||||
1
services/registration/pin_reset_mismatch_swa
Normal file
1
services/registration/pin_reset_mismatch_swa
Normal file
@@ -0,0 +1 @@
|
||||
PIN uliyoweka hailingani.Jaribu tena.
|
||||
1
services/registration/pin_reset_result_swa
Normal file
1
services/registration/pin_reset_result_swa
Normal file
@@ -0,0 +1 @@
|
||||
Ombi la kuweka upya PIN ya {{.retrieve_blocked_number}} limefanikiwa
|
||||
1
services/registration/retry_menu
Normal file
1
services/registration/retry_menu
Normal file
@@ -0,0 +1 @@
|
||||
Retry
|
||||
1
services/registration/retry_menu_swa
Normal file
1
services/registration/retry_menu_swa
Normal file
@@ -0,0 +1 @@
|
||||
Jaribu tena
|
||||
@@ -1,3 +1,4 @@
|
||||
CATCH no_voucher flag_no_active_voucher 1
|
||||
LOAD get_vouchers 0
|
||||
MAP get_vouchers
|
||||
MOUT back 0
|
||||
|
||||
1
services/registration/select_voucher_menu_swa
Normal file
1
services/registration/select_voucher_menu_swa
Normal file
@@ -0,0 +1 @@
|
||||
Chagua Sarafu
|
||||
1
services/registration/unregistered_number_swa
Normal file
1
services/registration/unregistered_number_swa
Normal file
@@ -0,0 +1 @@
|
||||
Nambari uliyoingiza haijasajiliwa na Sarafu au sio sahihi.
|
||||
1
services/registration/voucher_details_menu_swa
Normal file
1
services/registration/voucher_details_menu_swa
Normal file
@@ -0,0 +1 @@
|
||||
Maelezo ya Sarafu
|
||||
Reference in New Issue
Block a user