Skip to content

Commit

Permalink
Merge pull request #6 from Shivam010/dev
Browse files Browse the repository at this point in the history
feat: Makefile introduced, tests added
  • Loading branch information
Shivam010 authored Jun 23, 2020
2 parents 44f74e8 + 50b02cf commit 7310efd
Show file tree
Hide file tree
Showing 8 changed files with 306 additions and 37 deletions.
27 changes: 13 additions & 14 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build Check
name: build
on: [push]
jobs:

Expand All @@ -7,20 +7,19 @@ jobs:
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.12
uses: actions/setup-go@v1
with:
go-version: 1.12
id: go
- name: Set up Go 1.12
uses: actions/setup-go@v1
with:
go-version: 1.12
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v1
- name: Check out code into the Go module directory
uses: actions/checkout@v1

- name: Get dependencies
run: |
go get -v -t -d ./...
- name: Build
run: go build -v .
- name: Build and Test
# `test` firsts triggers `build` which triggers `clean`
# which in turns triggers `vet` and `fmt` as a series
# of operations
run: make test


22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
GO = GO111MODULE=on go

fmt:
${GO} fmt ./...

vet: fmt
${GO} vet ./...

clean: vet
rm -rf ./bin
${GO} mod tidy

build: clean
${GO} build -o ./bin/bypass-cors ./...

test: build
${GO} test -v -cover ./...

run: clean
${GO} run ./... -p 80

.PHONY: run build clean vet fmt
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/Shivam010/bypass-cors

go 1.12

require github.com/rs/cors v1.6.0
require github.com/google/go-cmp v0.3.1
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/rs/cors v1.6.0 h1:G9tHG9lebljV9mfp9SNPDL36nCDxmo3zTlAf1YgvzmI=
github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
41 changes: 28 additions & 13 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,38 +60,53 @@ func (e *Error) StatusCode() int {
return e.Code
}

const (
// headers
VaryHeader = "Vary"
OriginHeader = "Origin"
QuoteHeader = "quote"
// Access Control headers
AllowOrigin = "Access-Control-Allow-Origin"
AllowMethods = "Access-Control-Allow-Methods"
AllowHeaders = "Access-Control-Allow-Headers"
AllowCredentials = "Access-Control-Allow-Credentials"
// Access control request headers
RequestMethod = "Access-Control-Request-Method"
RequestHeaders = "Access-Control-Request-Headers"
)

// defaultHeaders handles a general request and add/set corresponding headers
func defaultHeaders(w http.ResponseWriter, r *http.Request) {
headers := w.Header()
origin := r.Header.Get("Origin")
origin := r.Header.Get(OriginHeader)

// Adding Vary header - for http cache
headers.Add("Vary", "Origin")
headers.Add(VaryHeader, OriginHeader)

// quote
headers.Set("quote", "Be Happy :)")
headers.Set(QuoteHeader, "Be Happy :)")

// Allowing only the requester - can be set to "*" too
headers.Set("Access-Control-Allow-Origin", origin)
headers.Set(AllowOrigin, origin)
// Always allowing credentials - just for the sake of proxy request
headers.Set("Access-Control-Allow-Credentials", "true")
headers.Set(AllowCredentials, "true")
}

// headersForPreflight handles the pre-flight cors request and add/set the
// corresponding headers
func headersForPreflight(w http.ResponseWriter, r *http.Request) {
headers := w.Header()
reqMethod := r.Header.Get("Access-Control-Request-Method")
reqHeaders := r.Header.Get("Access-Control-Request-Headers")
reqMethod := r.Header.Get(RequestMethod)
reqHeaders := r.Header.Get(RequestHeaders)

// Vary header - for http cache
headers.Add("Vary", "Access-Control-Request-Method")
headers.Add("Vary", "Access-Control-Request-Headers")
headers.Add(VaryHeader, RequestMethod)
headers.Add(VaryHeader, RequestHeaders)

// Allowing the requested method
headers.Set("Access-Control-Allow-Methods", strings.ToUpper(reqMethod))
headers.Set(AllowMethods, strings.ToUpper(reqMethod))
// Allowing the requested headers
headers.Set("Access-Control-Allow-Headers", reqHeaders)
headers.Set(AllowHeaders, reqHeaders)
}

// addHeaders handles request and set headers accordingly. It returns true if
Expand All @@ -100,7 +115,7 @@ func addHeaders(w http.ResponseWriter, r *http.Request) bool {

defaultHeaders(w, r)

if r.Method == http.MethodOptions && r.Header.Get("Access-Control-Request-Method") != "" {
if r.Method == http.MethodOptions && r.Header.Get(RequestMethod) != "" {
headersForPreflight(w, r)
Return(w, &ValuerStruct{Code: http.StatusOK})
return true
Expand All @@ -109,7 +124,7 @@ func addHeaders(w http.ResponseWriter, r *http.Request) bool {
return false
}

// getRequestURL returns the reuested URL to bypass-cors
// getRequestURL returns the requested URL to bypass-cors
func getRequestURL(w http.ResponseWriter, r *http.Request) *url.URL {

if r.URL.Path == "" || r.URL.Path == "/" {
Expand Down
24 changes: 24 additions & 0 deletions init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"flag"
)

const (
shortHand = " (short hand)"

defaultPort = "8080"
usagePort = "PORT at which the server will run"
)

var (
// PORT at which the server will run (default: 8080),
// can be modified using flags:
// `-port 80` or `-p 80`
PORT string
)

func init() {
flag.StringVar(&PORT, "port", defaultPort, usagePort)
flag.StringVar(&PORT, "p", defaultPort, usagePort+shortHand)
}
17 changes: 10 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io/ioutil"
"log"
"net/http"
"os"
)

type handler struct{}
Expand Down Expand Up @@ -60,6 +59,11 @@ func (*handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

fmt.Println("UserClient --> bypass-cors -->", req.URL.Host)

// Populate the rest of the header
for k, v := range r.Header {
req.Header.Add(k, v[0])
}

res, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println("Request Failed:", err)
Expand Down Expand Up @@ -97,14 +101,13 @@ func (*handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

func main() {
var PORT string
if PORT = os.Getenv("PORT"); PORT == "" {
flag.StringVar(&PORT, "p", "8080", "PORT at which the server will run")
}

// parse all flags set in `init`
flag.Parse()

fmt.Printf("\nRunning Proxy ByPass Cors Server at port = %v...\n\n", PORT)
fmt.Printf("\nStarting Proxy ByPass-Cors Server at port(:%v)...\n\n", PORT)

if err := http.ListenAndServe(":"+PORT, &handler{}); err != nil {
log.Println("\n\nPanic", err)
log.Println("\n\nPanics", err)
}
}
Loading

0 comments on commit 7310efd

Please sign in to comment.