aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--docker-compose-test.yaml2
-rw-r--r--hived/hived_test.go28
-rw-r--r--telebot/Dockerfile4
-rw-r--r--telebot/go.mod1
-rw-r--r--telebot/telebot.go83
-rwxr-xr-xtest/endpoints.sh12
6 files changed, 110 insertions, 20 deletions
diff --git a/docker-compose-test.yaml b/docker-compose-test.yaml
index d1772fb..b0030bc 100644
--- a/docker-compose-test.yaml
+++ b/docker-compose-test.yaml
@@ -21,6 +21,7 @@ services:
- ALL
environment:
- SERVER_DEPLOYMENT_TYPE=test
+ - ALL_PROXY=192.168.1.214:9995
telebot:
image: telebot
build:
@@ -36,6 +37,7 @@ services:
- ALL
environment:
- SERVER_DEPLOYMENT_TYPE=test
+ - ALL_PROXY=192.168.1.214:9995
arbiter:
image: arbiter
build:
diff --git a/hived/hived_test.go b/hived/hived_test.go
index bef135d..e9c45e4 100644
--- a/hived/hived_test.go
+++ b/hived/hived_test.go
@@ -40,10 +40,12 @@ func TestPriceHandler(t *testing.T) {
errorHandler(recorder, t, err)
var hivedPriceResponse HivedPriceResponse
+
bodyBytes, err := ioutil.ReadAll(recorder.Body)
if err != nil {
errorHandler(recorder, t, err)
}
+
err = json.Unmarshal(bodyBytes, &hivedPriceResponse)
if err != nil {
errorHandler(recorder, t, err)
@@ -62,10 +64,12 @@ func TestPairHandler(t *testing.T) {
errorHandler(recorder, t, err)
var hivedPairResponse HivedPairResponse
+
bodyBytes, err := ioutil.ReadAll(recorder.Body)
if err != nil {
errorHandler(recorder, t, err)
}
+
err = json.Unmarshal(bodyBytes, &hivedPairResponse)
if err != nil {
errorHandler(recorder, t, err)
@@ -81,26 +85,33 @@ func TestAlertHandlerPhase1(t *testing.T) {
defer rdb.Close()
postValues := map[string]string{"name": "alertTest", "expr": "ETH < 10000"}
+
postData, err := json.Marshal(postValues)
if err != nil {
fmt.Println(err.Error())
}
+
req, err := http.NewRequest(http.MethodPost, endpoint+"/alert", bytes.NewBuffer(postData))
+
req.Header.Set("Content-Type", "application/json")
+
recorder := httptest.NewRecorder()
alertHandler := AlertHandler{rdb: rdb}
alertHandler.HandleAlertPost(recorder, req)
errorHandler(recorder, t, err)
var hivedAlertGenericResponse HivedAlertGenericResponse
+
bodyBytes, err := ioutil.ReadAll(recorder.Body)
if err != nil {
errorHandler(recorder, t, err)
}
+
err = json.Unmarshal(bodyBytes, &hivedAlertGenericResponse)
if err != nil {
errorHandler(recorder, t, err)
}
+
if !hivedAlertGenericResponse.IsSuccessful {
fmt.Println(err.Error())
errorHandler(recorder, t, err)
@@ -122,14 +133,17 @@ func TestAlertHandlerPhase2(t *testing.T) {
errorHandler(recorder, t, err)
var hivedAlertGetResponse HivedAlertGetResponse
+
bodyBytes, err := ioutil.ReadAll(recorder.Body)
if err != nil {
errorHandler(recorder, t, err)
}
+
err = json.Unmarshal(bodyBytes, &hivedAlertGetResponse)
if err != nil {
errorHandler(recorder, t, err)
}
+
if !hivedAlertGetResponse.IsSuccessful {
fmt.Println(err.Error())
errorHandler(recorder, t, err)
@@ -145,12 +159,16 @@ func TestAlertHandlerPhase3(t *testing.T) {
defer rdb.Close()
postValues := map[string]string{"name": "alertTest", "expr": "ETH > 10000"}
+
postData, err := json.Marshal(postValues)
if err != nil {
fmt.Println(err.Error())
}
+
req, err := http.NewRequest(http.MethodPut, endpoint+"/alert", bytes.NewBuffer(postData))
+
req.Header.Set("Content-Type", "application/json")
+
recorder := httptest.NewRecorder()
alertHandler := AlertHandler{rdb: rdb}
alertHandler.HandleAlertGet(recorder, req)
@@ -172,14 +190,17 @@ func TestAlertHandlerPhase4(t *testing.T) {
errorHandler(recorder, t, err)
var hivedAlertGetResponse HivedAlertGetResponse
+
bodyBytes, err := ioutil.ReadAll(recorder.Body)
if err != nil {
errorHandler(recorder, t, err)
}
+
err = json.Unmarshal(bodyBytes, &hivedAlertGetResponse)
if err != nil {
errorHandler(recorder, t, err)
}
+
if !hivedAlertGetResponse.IsSuccessful {
fmt.Println(err.Error())
errorHandler(recorder, t, err)
@@ -201,14 +222,17 @@ func TestAlertHandlerPhase5(t *testing.T) {
errorHandler(recorder, t, err)
var hivedAlertGenericResponse HivedAlertGenericResponse
+
bodyBytes, err := ioutil.ReadAll(recorder.Body)
if err != nil {
errorHandler(recorder, t, err)
}
+
err = json.Unmarshal(bodyBytes, &hivedAlertGenericResponse)
if err != nil {
errorHandler(recorder, t, err)
}
+
if !hivedAlertGenericResponse.IsSuccessful {
fmt.Println(err.Error())
errorHandler(recorder, t, err)
@@ -224,20 +248,24 @@ func TestAlertHandlerPhase6(t *testing.T) {
defer rdb.Close()
req, err := http.NewRequest(http.MethodGet, endpoint+"/alert?key=alertTest", nil)
+
recorder := httptest.NewRecorder()
alertHandler := AlertHandler{rdb: rdb}
alertHandler.HandleAlertGet(recorder, req)
errorHandler(recorder, t, err)
var hivedAlertGetResponse HivedAlertGetResponse
+
bodyBytes, err := ioutil.ReadAll(recorder.Body)
if err != nil {
errorHandler(recorder, t, err)
}
+
err = json.Unmarshal(bodyBytes, &hivedAlertGetResponse)
if err != nil {
errorHandler(recorder, t, err)
}
+
if !hivedAlertGetResponse.IsSuccessful {
fmt.Println(err.Error())
errorHandler(recorder, t, err)
diff --git a/telebot/Dockerfile b/telebot/Dockerfile
index a3cebf9..e123c95 100644
--- a/telebot/Dockerfile
+++ b/telebot/Dockerfile
@@ -1,5 +1,7 @@
FROM alpine:3.16 as builder
ENV GOPROXY=https://goproxy.io
+ENV ALL_PROXY=socks5://192.168.1.214:9995
+ENV HTTPS_PROXY=socks5://192.168.1.214:9995
RUN apk update && apk upgrade
RUN apk add go git
ENV GOPROXY=https://goproxy.io
@@ -18,4 +20,6 @@ FROM alpine:3.16
COPY --from=certbuilder /certs /certs
COPY --from=builder /telebot/telebot /telebot/
COPY ./docker-entrypoint.sh /telebot/
+ENV ALL_PROXY=
+ENV HTTPS_PROXY=
ENTRYPOINT ["/telebot/docker-entrypoint.sh"]
diff --git a/telebot/go.mod b/telebot/go.mod
index d035e69..dab826a 100644
--- a/telebot/go.mod
+++ b/telebot/go.mod
@@ -7,5 +7,6 @@ require (
github.com/rs/zerolog v1.26.0
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
github.com/terminaldweller/grpc v1.0.3
+ golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d
google.golang.org/grpc v1.42.0
)
diff --git a/telebot/telebot.go b/telebot/telebot.go
index 0676ce9..d452519 100644
--- a/telebot/telebot.go
+++ b/telebot/telebot.go
@@ -5,57 +5,108 @@ import (
"flag"
"fmt"
"net"
+ "net/http"
"os"
"strconv"
+ "time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/rs/zerolog/log"
pb "github.com/terminaldweller/grpc/telebot/v1"
+ "golang.org/x/net/proxy"
"google.golang.org/grpc"
)
-var (
- flagPort = flag.String("port", "8000", "determined the port the sercice runs on")
-
- // FIXME-the client should provide the channel ID
- botChannelID = flag.Int64("botchannelid", 146328407, "determines the channel id the telgram bot should send messages to")
-)
+// FIXME-the client should provide the channel ID.
+var botChannelID = flag.Int64(
+ "botchannelid",
+ 146328407, //nolint: gomnd
+ "determines the channel id the telgram bot should send messages to")
const (
- TELEGRAM_BOT_TOKEN_ENV_VAR = "TELEGRAM_BOT_TOKEN"
- SERVER_DEPLOYMENT_TYPE = "SERVER_DEPLOYMENT_TYPE"
+ telegramBotTokenEnvVar = "TELEGRAM_BOT_TOKEN" //nolint: gosec
+ httpClientTimeout = 5
)
type server struct {
pb.UnimplementedNotificationServiceServer
}
+func GetProxiedClient() (*http.Client, error) {
+ proxyURL := os.Getenv("ALL_PROXY")
+ if proxyURL == "" {
+ proxyURL = os.Getenv("HTTPS_PROXY")
+ }
+
+ dialer, err := proxy.SOCKS5("tcp", proxyURL, nil, proxy.Direct)
+ if err != nil {
+ return nil, fmt.Errorf("[GetProxiedClient] : %w", err)
+ }
+
+ dialContext := func(ctx context.Context, network, address string) (net.Conn, error) {
+ netConn, err := dialer.Dial(network, address)
+ if err == nil {
+ return netConn, nil
+ }
+
+ return netConn, fmt.Errorf("[dialContext] : %w", err)
+ }
+
+ transport := &http.Transport{
+ DialContext: dialContext,
+ DisableKeepAlives: true,
+ }
+ client := &http.Client{
+ Transport: transport,
+ Timeout: httpClientTimeout * time.Second,
+ CheckRedirect: nil,
+ Jar: nil,
+ }
+
+ return client, nil
+}
+
func getTGBot() *tgbotapi.BotAPI {
- token := os.Getenv(TELEGRAM_BOT_TOKEN_ENV_VAR)
- bot, err := tgbotapi.NewBotAPI(token[1 : len(token)-1])
+ token := os.Getenv(telegramBotTokenEnvVar)
+
+ client, err := GetProxiedClient()
+ if err != nil {
+ log.Fatal().Err(err)
+ }
+
+ bot, err := tgbotapi.NewBotAPIWithClient(token[1:len(token)-1], client)
if err != nil {
- log.Error().Err(err)
+ log.Fatal().Err(err)
}
+
return bot
}
func sendMessage(bot *tgbotapi.BotAPI, msgText string, channelID int64) error {
msg := tgbotapi.NewMessage(channelID, msgText)
- bot.Send(msg)
- return nil
+ _, err := bot.Send(msg)
+
+ return err
}
-func (s *server) Notify(ctx context.Context, NotificationRequest *pb.NotificationRequest) (*pb.NotificationResponse, error) {
+func (s *server) Notify(
+ ctx context.Context,
+ NotificationRequest *pb.NotificationRequest,
+) (*pb.NotificationResponse, error) {
var err error
+
tgbotapi := getTGBot()
+
if NotificationRequest.ChannelId == 0 {
err = sendMessage(tgbotapi, NotificationRequest.NotificationText, *botChannelID)
} else {
err = sendMessage(tgbotapi, NotificationRequest.NotificationText, NotificationRequest.ChannelId)
}
+
if err != nil {
return &pb.NotificationResponse{Error: err.Error(), IsOK: false}, err
}
+
return &pb.NotificationResponse{Error: "", IsOK: true}, nil
}
@@ -69,13 +120,17 @@ func startServer(port uint16) {
grpcServer := grpc.NewServer(opts...)
pb.RegisterNotificationServiceServer(grpcServer, &server{})
+
if err := grpcServer.Serve(listener); err != nil {
log.Fatal().Err(err)
}
}
func main() {
+ flagPort := flag.String("port", "8000", "determines the port the service runs on")
flag.Parse()
+
port, _ := strconv.Atoi(*flagPort)
+
startServer(uint16(port))
}
diff --git a/test/endpoints.sh b/test/endpoints.sh
index 02381d6..7f9533b 100755
--- a/test/endpoints.sh
+++ b/test/endpoints.sh
@@ -3,10 +3,10 @@ set -e
set -x
# sleep 5
-curl -k -X GET "https://localhost:8008/crypto/price?name=CAKE&unit=USD"
-curl -k -X GET "https://localhost:8008/crypto/pair?one=ETH&two=CAKE&multiplier=4.0"
-curl -k -X POST -H "Content-Type: application/json" -d '{"name":"alert1", "expr":"ETH>CAKE"}' https://localhost:8008/crypto/alert
+curl -k -X GET "https://localhost:8008/crypto/v1/price?name=CAKE&unit=USD"
+curl -k -X GET "https://localhost:8008/crypto/v1/pair?one=ETH&two=CAKE&multiplier=4.0"
+curl -k -X POST -H "Content-Type: application/json" -d '{"name":"alert1", "expr":"ETH>CAKE"}' https://localhost:8008/crypto/v1/alert
-curl -X GET -H "Content-Type: application/json" "https://api.terminaldweller.com/crypto/alert?key=alert13"
-curl -X DELETE -H "Content-Type: application/json" "https://api.terminaldweller.com/crypto/alert?key=alert10"
-curl -X POST -H "Content-Type: application/json" -d '{"name":"alert1", "expr":"ETH>CAKE"}' https://api.terminaldweller.com/crypto/alert
+curl -k -X GET -H "Content-Type: application/json" "https://localhost:8008/crypto/v1/alert?key=alert1"
+curl -k -X DELETE -H "Content-Type: application/json" "https://localhost:8008/crypto/v1/alert?key=alert1"
+curl -k -X POST -H "Content-Type: application/json" -d '{"name":"alert1", "expr":"ETH>CAKE"}' https://localhost:8008/crypto/v1/alert