Commit 9ee157e9 authored by Your Name's avatar Your Name

add proxy

parent 885169f4
# FROM golang:1.22.3-alpine3.19 AS base
# WORKDIR /go/src/github.com/aiproxy
# COPY ./ ./
# ENV GOPROXY="https://goproxy.io,direct"
# # RUN go env -w GOPROXY=https://goproxy.cn,direct
# RUN go mod download
# FROM dep AS build
# RUN go build -o /proxy
# RUN --mount=target=. \
# --mount=type=cache,target=/go/pkg/mod \
# --mount=type=cache,target=/root/.cache/go-build \
# cd chain && make build-docker-ouput
# FROM cog-hello-world:latest
# COPY my_first_process my_first_process
# COPY my_second_process my_second_process
# COPY script.sh script.sh
# CMD ./script.sh
FROM cog-hello-world:latest
RUN apt-get update && apt-get install procps -y
COPY proxy proxy
# COPY my_second_process my_second_process
COPY script.sh script.sh
RUN chmod +x script.sh
CMD ./script.sh
\ No newline at end of file
FROM golang:1.22.3-alpine3.19 AS base
WORKDIR /go/src/github.com/aiproxy
COPY ./ ./
ENV GOPROXY="https://goproxy.io,direct"
# RUN go env -w GOPROXY=https://goproxy.cn,direct
RUN go mod download
# FROM base AS build
# RUN go build -o /proxy
# RUN --mount=target=. \
# --mount=type=cache,target=/go/pkg/mod \
# --mount=type=cache,target=/root/.cache/go-build \
# cd chain && make build-docker-ouput
# FROM cog-hello-world:latest
# COPY my_first_process my_first_process
# COPY my_second_process my_second_process
# COPY script.sh script.sh
# CMD ./script.sh
\ No newline at end of file
.PHONY: default metaring all clean fmt docker
VERSION := $(shell echo $(shell git describe --tags))
# dockerdep:
# docker build -f Dockerfile.dep -t dep .
docker:
docker build -f Dockerfile -t aiwithproxy:${VERSION} .
proxy:
rm -rf proxy
go build -o proxy
...@@ -73,16 +73,29 @@ paths: ...@@ -73,16 +73,29 @@ paths:
X-ProxyBodyHash-SignatureCode: X-ProxyBodyHash-SignatureCode:
type: integer type: integer
enum: [200, 500] enum: [200, 500]
description: sign status code. description: sign status code. eg 200 500
X-ProxyBodyHash-SignatureErr: X-ProxyBodyHash-SignatureErr:
type: string type: string
description: sign error msg. description: sign error msg.
X-ProxyBodyHash: X-ProxyBodyHash:
type: string type: string
description: response body hash value. description: response body hash value. "github.com/ethereum/go-ethereum/accounts" accounts.TextHash([]byte(string(c.Response().Body())))
X-ProxyBodyHash-Signature: X-ProxyBodyHash-Signature:
type: string type: string
description: signature for hash. description: signature for hash.
# -----------------------------------------------------
# Links
# -----------------------------------------------------
links:
GetUserByUserId: # <---- arbitrary name for the link
operationId: getUser
# or
# operationRef: '#/paths/~1users~1{userId}/get'
parameters:
userId: '$response.body#/id'
description: >
The `id` value returned in the response can be used as
the `userId` parameter in `GET /users/{userId}`.
content: content:
application/json: application/json:
schema: schema:
......
package main package main
import ( import (
"flag"
"fmt" "fmt"
"sync" "sync"
...@@ -10,74 +11,57 @@ import ( ...@@ -10,74 +11,57 @@ import (
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors" "github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/proxy" "github.com/gofiber/fiber/v2/middleware/proxy"
"github.com/gofiber/swagger"
// "github.com/gofiber/swagger"
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
) )
func main() { // "flag"
// "fmt"
// )
// func main() {
// var intParam int
// flag.IntVar(&intParam, "int", 0, "This is an int parameter")
// flag.Parse()
// app := fiber.New(fiber.Config{ func main() {
// // Prefork: true,
// // CaseSensitive: true,
// // StrictRouting: true,
// ServerHeader: "Fiber",
// AppName: "proxy with signature",
// })
// app := fiber.New() var listenPort int
// app.Use(cors.New()) flag.IntVar(&listenPort, "listenPort", 6000, "proxy listen the port")
var upstreamPort int
flag.IntVar(&upstreamPort, "upstreamPort", 5000, "forwar the request to the upstream port with localhost")
flag.Parse()
app := fiber.New() app := fiber.New()
app.Use(cors.New()) app.Use(cors.New())
app.Static("/swagger/docs", "./docs") // app.Static("/swagger/docs", "./docs")
// app.Get("/swagger/*", swagger.New(swagger.Config{ // custom
app.Get("/swagger/*", swagger.New(swagger.Config{ // custom // URL: "http://124.193.167.71:5001/swagger/docs/swagger.yaml", //http://124.193.167.71:8000/
URL: "http://124.193.167.71:5001/swagger/docs/swagger.yaml", //http://124.193.167.71:8000/ // DeepLinking: false,
DeepLinking: false, // // Expand ("list") or Collapse ("none") tag groups by default
// Expand ("list") or Collapse ("none") tag groups by default // DocExpansion: "none",
DocExpansion: "none", // }))
// Prefill OAuth ClientId on Authorize popup
// OAuth: &swagger.OAuthConfig{
// AppName: "OAuth Provider",
// ClientId: "21bb4edc-05a7-4afc-86f1-2e151e4ba6e2",
// },
// Ability to change OAuth2 redirect uri location
//OAuth2RedirectUrl: "http://localhost:8080/swagger/oauth2-redirect.html",
}))
app.All("/*", func(c *fiber.Ctx) error { app.All("/*", func(c *fiber.Ctx) error {
path := c.Path() path := c.Path()
fmt.Println("path", path) fmt.Println("path", path)
url := "http://124.193.167.71:5000" + path url := "http://127.0.0.1:" + fmt.Sprintf("%d", upstreamPort) + path
if err := proxy.Do(c, url); err != nil { if err := proxy.Do(c, url); err != nil {
return err return err
} }
// Remove Server header from response // Remove Server header from response
//c.Response().Header.Del(fiber.HeaderServer) //c.Response().Header.Del(fiber.HeaderServer)
//h := rlpHash(c.Response().Body())
//fmt.Printf("%x\n", accounts.TextHash([]byte("hello")))
//h := rlpHash("hello")
//h := accounts.TextHash([]byte("hello"))
h := accounts.TextHash([]byte(string(c.Response().Body()))) h := accounts.TextHash([]byte(string(c.Response().Body())))
fmt.Printf("%x\n", h) fmt.Printf("%x\n", h)
//prv *ecdsa.PrivateKey
// privateKey, _, err := getPrivateKeyByIdx(0)
// if err != nil {
// t.Fatal(err)
// }
fmt.Println("c.Response().Body():", string(c.Response().Body())) fmt.Println("c.Response().Body():", string(c.Response().Body()))
local, _ := crypto.HexToECDSA("4df7e0d1c1329d03c72ed0cec631adc048ae85178d3528e7f55368da0901ed5b") local, _ := crypto.HexToECDSA("4df7e0d1c1329d03c72ed0cec631adc048ae85178d3528e7f55368da0901ed5b")
...@@ -93,57 +77,18 @@ func main() { ...@@ -93,57 +77,18 @@ func main() {
c.Response().Header.Add("X-ProxyBodyHash-SignatureCode", "200") c.Response().Header.Add("X-ProxyBodyHash-SignatureCode", "200")
} }
//return tx.WithSignature(s, sig) //return tx.WithSignature(s, sig)
c.Response().Header.Add("X-ProxyBodyHash", fmt.Sprintf("%x", h)) c.Response().Header.Add("X-ProxyBodyHash", fmt.Sprintf("%x", h))
c.Response().Header.Add("X-ProxyBodyHash-Signature", fmt.Sprintf("%x", sig)) c.Response().Header.Add("X-ProxyBodyHash-Signature", fmt.Sprintf("%x", sig))
return nil return nil
}) })
if err := app.Listen(":5001"); err != nil { if err := app.Listen(fmt.Sprintf(":%d", listenPort)); err != nil {
fmt.Println(" app.Listen", err.Error()) fmt.Println(" app.Listen", err.Error())
} }
} }
// Hash returns the hash to be signed by the sender.
// It does not uniquely identify the transaction.
// func (s EIP155Signer) Hash(tx *Transaction) common.Hash {
// return rlpHash([]interface{}{
// tx.Nonce(),
// tx.GasPrice(),
// tx.Gas(),
// tx.To(),
// tx.Value(),
// tx.Data(),
// s.chainId, uint(0), uint(0),
// })
// }
// SignTx signs the transaction using the given signer and private key.
// func SignTx(tx *Transaction, s Signer, prv *ecdsa.PrivateKey) (*Transaction, error) {
// h := s.Hash(tx)
// sig, err := crypto.Sign(h[:], prv)
// if err != nil {
// return nil, err
// }
// return tx.WithSignature(s, sig)
// }
// Hash returns the hash to be signed by the sender.
// It does not uniquely identify the transaction.
// func (s EIP155Signer) Hash(tx *Transaction) common.Hash {
// return rlpHash([]interface{}{
// tx.Nonce(),
// tx.GasPrice(),
// tx.Gas(),
// tx.To(),
// tx.Value(),
// tx.Data(),
// s.chainId, uint(0), uint(0),
// })
// }
// rlpHash encodes x and hashes the encoded bytes. // rlpHash encodes x and hashes the encoded bytes.
func rlpHash(x interface{}) (h common.Hash) { func rlpHash(x interface{}) (h common.Hash) {
sha := hasherPool.Get().(crypto.KeccakState) sha := hasherPool.Get().(crypto.KeccakState)
...@@ -154,16 +99,6 @@ func rlpHash(x interface{}) (h common.Hash) { ...@@ -154,16 +99,6 @@ func rlpHash(x interface{}) (h common.Hash) {
return h return h
} }
// import (
// "bytes"
// "sync"
// "github.com/ethereum/go-ethereum/common"
// "github.com/ethereum/go-ethereum/crypto"
// "github.com/ethereum/go-ethereum/rlp"
// "golang.org/x/crypto/sha3"
// )
// hasherPool holds LegacyKeccak256 hashers for rlpHash. // hasherPool holds LegacyKeccak256 hashers for rlpHash.
var hasherPool = sync.Pool{ var hasherPool = sync.Pool{
New: func() interface{} { return sha3.NewLegacyKeccak256() }, New: func() interface{} { return sha3.NewLegacyKeccak256() },
......
This diff is collapsed.
File added
#!/bin/bash
nohup ./proxy &
nohup python -m cog.server.http &
while /bin/true; do
ps aux |grep proxy |grep -q -v grep
PROCESS_1_STATUS=$?
ps aux |grep cog.server.http |grep -q -v grep
PROCESS_2_STATUS=$?
# If the greps above find anything, they will exit with 0 status
# If they are not both 0, then something is wrong
if [ $PROCESS_1_STATUS -ne 0 -o $PROCESS_2_STATUS -ne 0 ]; then
echo "One of the processes has already exited."
exit -1
fi
sleep 60
done
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment