Skip to content

Commit

Permalink
merge: branch '3624-add-cors-support' into 'main'
Browse files Browse the repository at this point in the history
Added CORS support [#3624]

Closes #3624

See merge request accumulatenetwork/accumulate!1091
  • Loading branch information
Dennis B committed Sep 29, 2024
2 parents 698f0a4 + 5c90a86 commit a544825
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,13 @@ faucet-docker:
docker build --build-arg "GIT_DESCRIBE=$(GIT_DESCRIBE)" --build-arg "GIT_COMMIT=$(GIT_COMMIT)" -t "$(IMAGE)/faucet" -f cmd/accumulated-faucet/Dockerfile .

faucet-docker-push: faucet-docker
docker push "$(IMAGE)/faucet"
docker push "$(IMAGE)/faucet"

sim:
go build -trimpath $(FLAGS) ./tools/cmd/simulator

sim-docker:
docker build --build-arg "GIT_DESCRIBE=$(GIT_DESCRIBE)" --build-arg "GIT_COMMIT=$(GIT_COMMIT)" -t "$(IMAGE)/simulator" -f tools/cmd/simulator/Dockerfile .

sim-docker-push: sim-docker
docker push "$(IMAGE)/simulator"
25 changes: 25 additions & 0 deletions tools/cmd/simulator/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM golang:1.23 as build

ARG GIT_DESCRIBE
ARG GIT_COMMIT

# Build
WORKDIR /root
COPY . .
ENV CGO_ENABLED 0
RUN make sim GIT_DESCRIBE=$GIT_DESCRIBE GIT_COMMIT=$GIT_COMMIT
RUN go install github.com/go-delve/delve/cmd/dlv@latest
RUN mkdir /data

FROM alpine:3

# Install tools
RUN apk add --no-cache bash jq curl nano

# Copy binaries
COPY --from=build /root/simulator /go/bin/dlv /bin/

EXPOSE 26660

ENTRYPOINT ["simulator", "-b=1", "-v=1", "-s=100ms", "--database=/data", "--globals={\"executorVersion\": \"v2vandenberg\"}"]
CMD ["-a=0.0.0.0"]
81 changes: 80 additions & 1 deletion tools/cmd/simulator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"math/big"
"net"
"net/http"
"os"
"strings"
"time"

"github.com/AccumulateNetwork/jsonrpc2/v15"
Expand Down Expand Up @@ -43,9 +47,11 @@ var flag = struct {
LogFormat string
Step string
Globals string
BaseAddr string
BvnCount int
ValCount int
BasePort int
Cors []string
}{}

func init() {
Expand All @@ -59,10 +65,28 @@ func init() {
cmd.Flags().IntVarP(&flag.BvnCount, "bvns", "b", 3, "Number of BVNs to create; applicable only when --network=simple")
cmd.Flags().IntVarP(&flag.ValCount, "validators", "v", 3, "Number of validators to create per BVN; applicable only when --network=simple")
cmd.Flags().IntVarP(&flag.BasePort, "port", "p", 26656, "Base port to listen on")
cmd.Flags().StringVarP(&flag.BaseAddr, "address", "a", "127.0.1.1", "Base address to listen on")
cmd.Flags().StringSliceVarP(&flag.Cors, "cors", "c", []string{"*"}, "Specify url's for CORS requests, (default=*)")

cmd.MarkFlagsMutuallyExclusive("snapshot", "globals")
}

func findLoopback() (ret []net.IP) {
ips, err := net.LookupIP("localhost")
if err != nil {
log.Fatalf("Could not resolve localhost: %v\n", err)
}

// Find and print the default 127.x.x.x address (typically 127.0.0.1)
for i, ip := range ips {
if ip.To4() != nil && ip.IsLoopback() {
fmt.Printf("Default 127.x.x.x address: %s\n", ip.String())
ret = append(ret, ips[i])
}
}
return ret
}

var DefaultLogLevels = config.LogLevel{}.
Parse(config.DefaultLogLevels).
SetModule("sim", "info").
Expand All @@ -71,7 +95,35 @@ var DefaultLogLevels = config.LogLevel{}.

func main() { _ = cmd.Execute() }

func nextIP(ip net.IP, addToIP int) net.IP {
// Convert IP to a big.Int
ipInt := big.NewInt(0).SetBytes(ip.To16()) // To16 ensures it works for both IPv4 and IPv6

// Add 1 to the IP address
ipInt.Add(ipInt, big.NewInt(int64(addToIP)))

// Convert back to IP
newIP := ipInt.Bytes()

// Handle IPv4 by slicing the last 4 bytes
if ip.To4() != nil {
return net.IP(newIP[len(newIP)-4:])
}
return net.IP(newIP)
}

func run(*cobra.Command, []string) {
var baseAddr net.IP
if flag.BaseAddr == "localhost" {
ips := findLoopback()
if len(ips) == 0 {
log.Fatal("No IP addresses found")
}
baseAddr = ips[0]
} else {
baseAddr = net.ParseIP(flag.BaseAddr)
}

jsonrpc2.DebugMethodFunc = true

var opts []simulator.Option
Expand All @@ -84,7 +136,7 @@ func run(*cobra.Command, []string) {
net = simulator.NewSimpleNetwork("Simulator", flag.BvnCount, flag.ValCount)
for i, bvn := range net.Bvns {
for j, node := range bvn.Nodes {
node.AdvertizeAddress = fmt.Sprintf("127.0.1.%d", 1+i*flag.ValCount+j)
node.AdvertizeAddress = nextIP(baseAddr, i*flag.ValCount+j).String()
node.BasePort = uint64(flag.BasePort)
}
}
Expand Down Expand Up @@ -128,6 +180,9 @@ func run(*cobra.Command, []string) {
ListenHTTPv3: true,
ServeError: check,
HookHTTP: func(h http.Handler, w http.ResponseWriter, r *http.Request) {
if handleCORS(w, r) {
return
}
onWaitHook(sim, h, w, r)
},
}))
Expand All @@ -149,6 +204,12 @@ func run(*cobra.Command, []string) {
ListenHTTPv2: true,
ListenHTTPv3: true,
ServeError: check,
HookHTTP: func(h http.Handler, w http.ResponseWriter, r *http.Request) {
if handleCORS(w, r) {
return
}
h.ServeHTTP(w, r)
},
}))

select {}
Expand Down Expand Up @@ -250,3 +311,21 @@ func waitForTxID(sim *simulator.Simulator, txid *url.TxID, ignorePending bool) {
}
}
}

func handleCORS(w http.ResponseWriter, r *http.Request) bool {
if flag.Cors == nil {
return false
}

cors := strings.Join(flag.Cors, ",")
w.Header().Set("Access-Control-Allow-Origin", cors) // set "*" for testing, but use specific origin in production
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")

// Handle preflight OPTIONS request
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return true
}
return false
}

0 comments on commit a544825

Please sign in to comment.