func Handle(w http.ResponseWriter, r *http.Request) {
	responses.AddJSONContentType(w)

	if r.Body == nil {
		w.WriteHeader(http.StatusBadRequest)
		w.Write(rpcerrors.NewJSONParseError(errors.Err("empty request body")).JSON())
		logger.Log().Debugf("empty request body")
		return
	}

	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		w.Write(rpcerrors.NewJSONParseError(errors.Err("error reading request body")).JSON())
		logger.Log().Debugf("error reading request body: %v", err.Error())
		return
	}

	var rpcReq *jsonrpc.RPCRequest
	err = json.Unmarshal(body, &rpcReq)
	if err != nil {
		w.Write(rpcerrors.NewJSONParseError(err).JSON())
		return
	}

	logger.Log().Tracef("call to method %s", rpcReq.Method)

	user, err := auth.FromRequest(r)
	if query.MethodRequiresWallet(rpcReq.Method) {
		authErr := GetAuthError(user, err)
		if authErr != nil {
			w.Write(rpcerrors.ErrorToJSON(authErr))
			return
		}
	}

	var userID int
	if query.MethodAcceptsWallet(rpcReq.Method) && user != nil {
		userID = user.ID
	}

	sdkAddress := sdkrouter.GetSDKAddress(user)
	if sdkAddress == "" {
		rt := sdkrouter.FromRequest(r)
		sdkAddress = rt.RandomServer().Address
	}

	var qCache cache.QueryCache
	if cache.IsOnRequest(r) {
		qCache = cache.FromRequest(r)
	}
	c := query.NewCaller(sdkAddress, userID)

	remoteIP := ip.FromRequest(r)
	// Logging remote IP with query
	c.AddPostflightHook("wallet_", func(_ *query.Caller, hctx *query.HookContext) (*jsonrpc.RPCResponse, error) {
		hctx.AddLogField("remote_ip", remoteIP)
		return nil, nil
	}, "")
	c.AddPostflightHook(query.MethodWalletSend, func(_ *query.Caller, hctx *query.HookContext) (*jsonrpc.RPCResponse, error) {
		audit.LogQuery(userID, remoteIP, query.MethodWalletSend, body)
		return nil, nil
	}, "")
	c.Cache = qCache

	rpcRes, err := c.Call(rpcReq)

	if err != nil {
		monitor.ErrorToSentry(err, map[string]string{"request": fmt.Sprintf("%+v", rpcReq), "response": fmt.Sprintf("%+v", rpcRes)})
		logger.Log().Errorf("error calling lbrynet: %v, request: %+v", err, rpcReq)
		w.Write(rpcerrors.ToJSON(err))
		return
	}

	serialized, err := responses.JSONRPCSerialize(rpcRes)
	if err != nil {
		monitor.ErrorToSentry(err)
		logger.Log().Errorf("error marshaling response: %v", err)
		w.Write(rpcerrors.NewInternalError(err).JSON())
		return
	}

	w.Write(serialized)
}
// Handle forwards client JSON-RPC request to proxy.
Handle forwards client JSON-RPC request to proxy.
// Handle forwards client JSON - RPC request to proxy .
func Handle(w http.ResponseWriter, r *http.Request) {
	responses.AddJSONContentType(w)
	if r.Body == nil {
		w.WriteHeader(http.StatusBadRequest)
		w.Write(rpcerrors.NewJSONParseError(errors.Err("empty request body")).JSON())
		logger.Log().Debugf("empty request body")
		return
	}
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		w.Write(rpcerrors.NewJSONParseError(errors.Err("error reading request body")).JSON())
		logger.Log().Debugf("error reading request body: %v", err.Error())
		return
	}
	var rpcReq *jsonrpc.RPCRequest
	err = json.Unmarshal(body, &rpcReq)
	if err != nil {
		w.Write(rpcerrors.NewJSONParseError(err).JSON())
		return
	}
	logger.Log().Tracef("call to method %s", rpcReq.Method)
	user, err := auth.FromRequest(r)
	if query.MethodRequiresWallet(rpcReq.Method) {
		authErr := GetAuthError(user, err)
		if authErr != nil {
			w.Write(rpcerrors.ErrorToJSON(authErr))
			return
		}
	}
	var userID int
	if query.MethodAcceptsWallet(rpcReq.Method) && user != nil {
		userID = user.ID
	}
	sdkAddress := sdkrouter.GetSDKAddress(user)
	if sdkAddress == "" {
		rt := sdkrouter.FromRequest(r)
		sdkAddress = rt.RandomServer().Address
	}
	var qCache cache.QueryCache
	if cache.IsOnRequest(r) {
		qCache = cache.FromRequest(r)
	}
	c := query.NewCaller(sdkAddress, userID)
	remoteIP := ip.FromRequest(r)
	c.AddPostflightHook("wallet_", func(_ *query.Caller, hctx *query.HookContext) (*jsonrpc.RPCResponse, error) {
		hctx.AddLogField("remote_ip", remoteIP)
		return nil, nil
	}, "")
	c.AddPostflightHook(query.MethodWalletSend, func(_ *query.Caller, hctx *query.HookContext) (*jsonrpc.RPCResponse, error) {
		audit.LogQuery(userID, remoteIP, query.MethodWalletSend, body)
		return nil, nil
	}, "")
	c.Cache = qCache
	rpcRes, err := c.Call(rpcReq)
	if err != nil {
		monitor.ErrorToSentry(err, map[string]string{"request": fmt.Sprintf("%+v", rpcReq), "response": fmt.Sprintf("%+v", rpcRes)})
		logger.Log().Errorf("error calling lbrynet: %v, request: %+v", err, rpcReq)
		w.Write(rpcerrors.ToJSON(err))
		return
	}
	serialized, err := responses.JSONRPCSerialize(rpcRes)
	if err != nil {
		monitor.ErrorToSentry(err)
		logger.Log().Errorf("error marshaling response: %v", err)
		w.Write(rpcerrors.NewInternalError(err).JSON())
		return
	}
	w.Write(serialized)
}
func Handle ( w http . ResponseWriter , r * http . Request ) { responses . AddJSONContentType ( w ) if r . Body == nil { w . WriteHeader ( http . StatusBadRequest ) w . Write ( rpcerrors . NewJSONParseError ( errors . Err ( " " ) ) . JSON ( ) ) logger . Log ( ) . Debugf ( " " ) return } body , err := ioutil . ReadAll ( r . Body ) if err != nil { w . WriteHeader ( http . StatusBadRequest ) w . Write ( rpcerrors . NewJSONParseError ( errors . Err ( " " ) ) . JSON ( ) ) logger . Log ( ) . Debugf ( " " , err . Error ( ) ) return } var rpcReq * jsonrpc . RPCRequest err = json . Unmarshal ( body , & rpcReq ) if err != nil { w . Write ( rpcerrors . NewJSONParseError ( err ) . JSON ( ) ) return } logger . Log ( ) . Tracef ( " " , rpcReq . Method ) user , err := auth . FromRequest ( r ) if query . MethodRequiresWallet ( rpcReq . Method ) { authErr := GetAuthError ( user , err ) if authErr != nil { w . Write ( rpcerrors . ErrorToJSON ( authErr ) ) return } } var userID int if query . MethodAcceptsWallet ( rpcReq . Method ) && user != nil { userID = user . ID } sdkAddress := sdkrouter . GetSDKAddress ( user ) if sdkAddress == " " { rt := sdkrouter . FromRequest ( r ) sdkAddress = rt . RandomServer ( ) . Address } var qCache cache . QueryCache if cache . IsOnRequest ( r ) { qCache = cache . FromRequest ( r ) } c := query . NewCaller ( sdkAddress , userID ) remoteIP := ip . FromRequest ( r ) c . AddPostflightHook ( " " , func ( _ * query . Caller , hctx * query . HookContext ) ( * jsonrpc . RPCResponse , error ) { hctx . AddLogField ( " " , remoteIP ) return nil , nil } , " " ) c . AddPostflightHook ( query . MethodWalletSend , func ( _ * query . Caller , hctx * query . HookContext ) ( * jsonrpc . RPCResponse , error ) { audit . LogQuery ( userID , remoteIP , query . MethodWalletSend , body ) return nil , nil } , " " ) c . Cache = qCache rpcRes , err := c . Call ( rpcReq ) if err != nil { monitor . ErrorToSentry ( err , map [ string ] string { " " : fmt . Sprintf ( " " , rpcReq ) , " " : fmt . Sprintf ( " " , rpcRes ) } ) logger . Log ( ) . Errorf ( " " , err , rpcReq ) w . Write ( rpcerrors . ToJSON ( err ) ) return } serialized , err := responses . JSONRPCSerialize ( rpcRes ) if err != nil { monitor . ErrorToSentry ( err ) logger . Log ( ) . Errorf ( " " , err ) w . Write ( rpcerrors . NewInternalError ( err ) . JSON ( ) ) return } w . Write ( serialized ) }
Handle forwards client JSON-RPC request to proxy.
Handle forwards client JSON - RPC request to proxy .
// Logging remote IP with query