Initial commit
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
module aws-sms
|
||||
|
||||
go 1.24.4
|
||||
|
||||
require (
|
||||
github.com/aws/aws-sdk-go-v2 v1.41.9 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/config v1.32.20 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.19.19 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.25 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.25 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.25 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.26 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.10 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.25 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/signin v1.1.1 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sns v1.39.19 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.30.19 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.36.2 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.42.3 // indirect
|
||||
github.com/aws/smithy-go v1.26.0 // indirect
|
||||
)
|
||||
@@ -0,0 +1,30 @@
|
||||
github.com/aws/aws-sdk-go-v2 v1.41.9 h1:/rYeyO2+HrMztAmxAq9++XJtFMqSIpSsNA0yDGALYq4=
|
||||
github.com/aws/aws-sdk-go-v2 v1.41.9/go.mod h1:+HsoOEX80qAVUitj1A2DhCNTjmb3edVyuDypb6LNEeo=
|
||||
github.com/aws/aws-sdk-go-v2/config v1.32.20 h1:8VMDnWc/kEzxsI/1ngGM9mG81a8IGmIHD8KLcYGwagc=
|
||||
github.com/aws/aws-sdk-go-v2/config v1.32.20/go.mod h1:PuwEpciweIXGULWeOeSTXtSbH4CW9mWdWrhdCKQI1sM=
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.19.19 h1:yuFzSV1U0aRNYCQGVaTY2zW2M/L93pYHnXnrJUphYhU=
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.19.19/go.mod h1:7y63L1kGzeoDlJaQ3Z578KrnmfBut96JjvJUzGwR+YE=
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.25 h1:0w6dCiO8iez+YKwRhRBlL1CH/E3GTfdkuzrwj1by8vo=
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.25/go.mod h1:9FDWUothyr5RCRAHc45XOiVCzUR8n/IhCYX+uVqw6vk=
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.25 h1:Uii3frf9ztec/ABM2/FSH9/z7PLzxfpG8h4RpkUFflQ=
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.25/go.mod h1:G6kntsA2GorAxDPbap6xgB2F+amSLUF8GJTi7PUoX44=
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.25 h1:r1+/l6m+WaUJF9HISEsNOLHSNj5EXYQxK8VX6Cz9NlA=
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.25/go.mod h1:cKf+D+NMDK1LndD7BowHbBZPgR9V0/5HubH0PFWvA+c=
|
||||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.26 h1:A1PmWU2zfkIm9EyFlJncFXL4W4phML+h8KjltUsCvNQ=
|
||||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.26/go.mod h1:dY4MRzXEizrD4hqtpKvWVGPX7QleSGGVY+EBolo1RmM=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.10 h1:d5/908OJ4bXg8lyjeMPvXetEKqoDoLi5Owy1zNue3yg=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.10/go.mod h1:a57l7Hwh+FWI+we50g5NPJHYUKeJKfXbc4w8SyXu8Ig=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.25 h1:dD3dhHNglpd98gs72my22Ndqi1hqQGllFFg1F+twfxg=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.25/go.mod h1:0yAbjPfd64gG7mj85RW+fMEYdfBgCRZw8g/oWcL1pjc=
|
||||
github.com/aws/aws-sdk-go-v2/service/signin v1.1.1 h1:1VwbP3qMNfxUDEXWki4rCE5iA+44VA1lokTz9HasGzw=
|
||||
github.com/aws/aws-sdk-go-v2/service/signin v1.1.1/go.mod h1:vUtyoSj0OPji3kjIVSc/GlKuWEiL33f/WFxl6dmpy/A=
|
||||
github.com/aws/aws-sdk-go-v2/service/sns v1.39.19 h1:FFhX5wY9zHX1IzSsqHlcd9TZgejkF5+F/SpvWZcdS+k=
|
||||
github.com/aws/aws-sdk-go-v2/service/sns v1.39.19/go.mod h1:1L0Y96eKbF+uIfA/m6JagGDBprXP8Bzz7fUjjmVCI7A=
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.30.19 h1:N6pIsdFOW1Kd9S4KyFKXdGRBojPPxkP32+uHFWLv4Hc=
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.30.19/go.mod h1:3gt5WJArFooNmyLONS+h/R4J+o86II8du38IgCwj9dE=
|
||||
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.36.2 h1:hc+lBYiiTr8Zk4MTzIsQ92MeDWCIDvWGmzKUWOaBcOg=
|
||||
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.36.2/go.mod h1:hU6fqB3OJA6/ePheD47LQnxvjYk6br6PtQxs+Q9ojvk=
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.42.3 h1:ErklX/7uhSbkAAeyQD/Y1OoQ9hO3SJXQNEgksORW3Js=
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.42.3/go.mod h1:ULe4HCzfKPiR6R3HEurE3b1upEkuk8AkMrOKtaOxKO8=
|
||||
github.com/aws/smithy-go v1.26.0 h1:9ouqbi+NyKP7fV3Te7UElCwdAb6Y8uk7LGwPE5tVe/s=
|
||||
github.com/aws/smithy-go v1.26.0/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc=
|
||||
@@ -0,0 +1,205 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
"os"
|
||||
"context"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/config"
|
||||
"github.com/aws/aws-sdk-go-v2/service/sns"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
requestData requestData
|
||||
}
|
||||
|
||||
type requestData struct {
|
||||
method string
|
||||
protocol string
|
||||
content_length int64
|
||||
transfer_encoding []string
|
||||
remote_address string
|
||||
remote_port string
|
||||
request_uri string
|
||||
//tls_metadata http.tls.ConnectionState
|
||||
headers http.Header
|
||||
//body string
|
||||
body []byte
|
||||
//form_data http.url.Values
|
||||
url string
|
||||
}
|
||||
|
||||
type SMS struct {
|
||||
Message string `json:"message"`
|
||||
DestNo string `json:"to"`
|
||||
}
|
||||
|
||||
func sendResponse(w http.ResponseWriter) {
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
|
||||
//fmt.Fprintln(w, "Hello World!")
|
||||
}
|
||||
|
||||
func sendResponse2(w http.ResponseWriter) {
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
//fmt.Fprintln(w, "Hello World!")
|
||||
}
|
||||
|
||||
func sendResponse3(w http.ResponseWriter) {
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
|
||||
//fmt.Fprintln(w, "Hello World!")
|
||||
}
|
||||
|
||||
func checkIP(reqData *requestData, w http.ResponseWriter) int {
|
||||
var whitelisted_ip []string
|
||||
|
||||
whitelisted_ip = append(whitelisted_ip, "105.233.34.134")
|
||||
whitelisted_ip = append(whitelisted_ip, "156.38.200.7")
|
||||
|
||||
for _,ip:= range whitelisted_ip {
|
||||
if ip == reqData.remote_address {
|
||||
fmt.Println("Match found! ", reqData.remote_address)
|
||||
sendResponse2(w)
|
||||
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
sendResponse(w)
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
func sendSMS(reqData *requestData, sms_deets *SMS, w http.ResponseWriter) int {
|
||||
//err := json.Unmarshal(reqData.body, &sms_deets)
|
||||
//if err != nil {
|
||||
//sendResponse3(w)
|
||||
|
||||
// return 1
|
||||
//}
|
||||
|
||||
err := os.Setenv("AWS_ACCESS_KEY", "AKIARTV6SB2DHXEZW553")
|
||||
if err != nil {
|
||||
// log error
|
||||
sendResponse3(w)
|
||||
}
|
||||
|
||||
err = os.Setenv("AWS_SECRET_ACCESS_KEY", "h7KFrorkmPY1jWwO2foOi24aRHEYFS0XxQIjituv")
|
||||
if err != nil {
|
||||
// log error
|
||||
sendResponse3(w)
|
||||
}
|
||||
|
||||
err = os.Setenv("AWS_REGION", "eu-north-1")
|
||||
if err != nil {
|
||||
// log error
|
||||
sendResponse3(w)
|
||||
}
|
||||
|
||||
cfg, err2 := config.LoadDefaultConfig(context.TODO(), config.WithRegion("eu-north-1"))
|
||||
if err2 != nil {
|
||||
fmt.Println("CONFIG ERROR!!!")
|
||||
}
|
||||
|
||||
client := sns.NewFromConfig(cfg)
|
||||
|
||||
input := &sns.PublishInput {
|
||||
Message: aws.String(sms_deets.Message),
|
||||
PhoneNumber: aws.String(sms_deets.DestNo),
|
||||
}
|
||||
|
||||
result, err3 := client.Publish(context.TODO(), input)
|
||||
if err3 != nil {
|
||||
fmt.Println("ASDAD")
|
||||
} else {
|
||||
fmt.Println(*result.MessageId)
|
||||
}
|
||||
|
||||
|
||||
|
||||
fmt.Println("\n")
|
||||
fmt.Println("Form Data")
|
||||
fmt.Println("=========")
|
||||
fmt.Println("Json Message: ", sms_deets.Message)
|
||||
fmt.Println("Json DestNo: ", sms_deets.DestNo)
|
||||
fmt.Println("\n")
|
||||
|
||||
// Decode the incoming json request
|
||||
/*decoder := json.NewDecoder(r.Body)
|
||||
_ = decoder
|
||||
err := decoder.Decode(&sms_deets)
|
||||
if err != nil {
|
||||
fmt.Println("ERROR!")
|
||||
}
|
||||
defer r.Body.Close()*/
|
||||
|
||||
os.Unsetenv("AWS_ACCESS_KEY")
|
||||
os.Unsetenv("AWS_SECRET_ACCESS_KEY")
|
||||
os.Unsetenv("AWS_REGION")
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func getPostData(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// add logging
|
||||
|
||||
meh := strings.Split(r.RemoteAddr, ":")
|
||||
ip := meh[0]
|
||||
port := meh[1]
|
||||
|
||||
reqData := &requestData {
|
||||
method: r.Method,
|
||||
protocol: r.Proto,
|
||||
content_length: r.ContentLength,
|
||||
transfer_encoding: r.TransferEncoding,
|
||||
remote_address: ip,
|
||||
remote_port: port,
|
||||
request_uri: r.RequestURI,
|
||||
//tls_metadata: r.TLS,
|
||||
headers: r.Header,
|
||||
body: nil,
|
||||
url: r.URL.String(),
|
||||
//r.postForm: r.PostForm,
|
||||
}
|
||||
|
||||
// Check if the source ip is whitelisted
|
||||
e := checkIP(reqData, w)
|
||||
if e > 0 {
|
||||
return
|
||||
}
|
||||
|
||||
sms_deets := new (SMS)
|
||||
|
||||
// Decode the incoming json request
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
err := decoder.Decode(&sms_deets)
|
||||
if err != nil {
|
||||
// Log this error fmt.Println("ERROR!")
|
||||
sendResponse3(w)
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
sendSMS(reqData, sms_deets, w)
|
||||
}
|
||||
|
||||
func main () {
|
||||
http.HandleFunc("/send_sms", getPostData)
|
||||
|
||||
fmt.Println("Starting HTTPS Server...")
|
||||
|
||||
http.ListenAndServeTLS(":8443", "/etc/letsencrypt/live/prd-jhb-ep01.cm-ha.co.za/fullchain.pem", "/etc/letsencrypt/live/prd-jhb-ep01.cm-ha.co.za/privkey.pem", nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user