Unverified Commit 013702bb authored by Simon Esposito's avatar Simon Esposito Committed by GitHub
Browse files

Add runtime http insecure flag (#1002)

parent 9fadf79b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr
## [Unreleased]
### Added
- Add tournament record delete runtime functions and API.
- Add insecure flag to runtime http functions to optionally skip TLS checks.

### Changed
- Improve graceful shutdown of Google IAP receipt processor.
+15 −1
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/sha256"
	"crypto/tls"
	"crypto/x509"
	"database/sql"
	"encoding/base64"
@@ -63,6 +64,7 @@ type runtimeJavascriptNakamaModule struct {
	protojsonMarshaler   *protojson.MarshalOptions
	protojsonUnmarshaler *protojson.UnmarshalOptions
	httpClient           *http.Client
	httpClientInsecure   *http.Client
	socialClient         *social.Client
	leaderboardCache     LeaderboardCache
	rankCache            LeaderboardRankCache
@@ -104,6 +106,7 @@ func NewRuntimeJavascriptNakamaModule(logger *zap.Logger, db *sql.DB, protojsonM
		localCache:           localCache,
		leaderboardScheduler: leaderboardScheduler,
		httpClient:           &http.Client{},
		httpClientInsecure:   &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}},

		node:          config.GetName(),
		eventFn:       eventFn,
@@ -555,6 +558,7 @@ func (n *runtimeJavascriptNakamaModule) sqlQuery(r *goja.Runtime) func(goja.Func
// @param headers(type=string) A table of headers used with the request.
// @param content(type=string) The bytes to send with the request.
// @param timeout(type=number, optional=true, default=5000) Timeout of the request in milliseconds.
// @param insecure(type=bool, optional=true, default=false) Set to true to skip request TLS validations.
// @return returnVal(nkruntime.httpResponse) Code, Headers, and Body response values for the HTTP response.
// @return error(error) An optional error value if an error occurred.
func (n *runtimeJavascriptNakamaModule) httpRequest(r *goja.Runtime) func(goja.FunctionCall) goja.Value {
@@ -581,6 +585,11 @@ func (n *runtimeJavascriptNakamaModule) httpRequest(r *goja.Runtime) func(goja.F
			timeoutMs = 5_000
		}

		var insecure bool
		if !goja.IsUndefined(f.Argument(5)) && !goja.IsNull(f.Argument(5)) {
			insecure = getJsBool(r, f.Argument(5))
		}

		if url == "" {
			panic(r.NewTypeError("URL string cannot be empty."))
		}
@@ -614,7 +623,12 @@ func (n *runtimeJavascriptNakamaModule) httpRequest(r *goja.Runtime) func(goja.F
			req.Header.Add(h, v)
		}

		resp, err := n.httpClient.Do(req)
		var resp *http.Response
		if insecure {
			resp, err = n.httpClientInsecure.Do(req)
		} else {
			resp, err = n.httpClient.Do(req)
		}
		if err != nil {
			panic(r.NewGoError(fmt.Errorf("HTTP request error: %v", err.Error())))
		}
+16 −3
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/sha256"
	"crypto/tls"
	"crypto/x509"
	"database/sql"
	"encoding/base64"
@@ -81,7 +82,8 @@ type RuntimeLuaNakamaModule struct {
	localCache           *RuntimeLuaLocalCache
	registerCallbackFn   func(RuntimeExecutionMode, string, *lua.LFunction)
	announceCallbackFn   func(RuntimeExecutionMode, string)
	client               *http.Client
	httpClient           *http.Client
	httpClientInsecure   *http.Client

	node          string
	matchCreateFn RuntimeMatchCreateFunction
@@ -112,7 +114,8 @@ func NewRuntimeLuaNakamaModule(logger *zap.Logger, db *sql.DB, protojsonMarshale
		localCache:           localCache,
		registerCallbackFn:   registerCallbackFn,
		announceCallbackFn:   announceCallbackFn,
		client:               &http.Client{},
		httpClient:           &http.Client{},
		httpClientInsecure:   &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}},

		node:          config.GetName(),
		matchCreateFn: matchCreateFn,
@@ -920,6 +923,7 @@ func (n *RuntimeLuaNakamaModule) uuidStringToBytes(l *lua.LState) int {
// @param headers(type=table, optional=true) A table of headers used with the request.
// @param content(type=string, optional=true) The bytes to send with the request.
// @param timeout(type=number, optional=true, default=5000) Timeout of the request in milliseconds.
// @param insecure(type=bool, optional=true, default=false) Set to true to skip request TLS validations.
// @return returnVal(table) Code, Headers, and Body response values for the HTTP response.
// @return error(error) An optional error value if an error occurred.
func (n *RuntimeLuaNakamaModule) httpRequest(l *lua.LState) int {
@@ -951,6 +955,8 @@ func (n *RuntimeLuaNakamaModule) httpRequest(l *lua.LState) int {
		timeoutMs = 5_000
	}

	insecure := l.OptBool(6, false)

	// Prepare request body, if any.
	var requestBody io.Reader
	if body != "" {
@@ -966,6 +972,7 @@ func (n *RuntimeLuaNakamaModule) httpRequest(l *lua.LState) int {
		l.RaiseError("HTTP request error: %v", err.Error())
		return 0
	}

	// Apply any request headers.
	httpHeaders := RuntimeLuaConvertLuaTable(headers)
	for k, v := range httpHeaders {
@@ -976,8 +983,14 @@ func (n *RuntimeLuaNakamaModule) httpRequest(l *lua.LState) int {
		}
		req.Header.Add(k, vs)
	}

	// Execute the request.
	resp, err := n.client.Do(req)
	var resp *http.Response
	if insecure {
		resp, err = n.httpClientInsecure.Do(req)
	} else {
		resp, err = n.httpClient.Do(req)
	}
	if err != nil {
		l.RaiseError("HTTP request error: %v", err.Error())
		return 0