MCPcopy Create free account
hub / github.com/CPChain/chain / wsHandshakeValidator

Function wsHandshakeValidator

api/rpc/websocket.go:86–119  ·  view source on GitHub ↗

wsHandshakeValidator returns a handler that verifies the origin during the websocket upgrade process. When a '*' is specified as an allowed origins all connections are accepted.

(allowedOrigins []string)

Source from the content-addressed store, hash-verified

84// websocket upgrade process. When a '*' is specified as an allowed origins all
85// connections are accepted.
86func wsHandshakeValidator(allowedOrigins []string) func(*websocket.Config, *http.Request) error {
87 origins := set.New()
88 allowAllOrigins := false
89
90 for _, origin := range allowedOrigins {
91 if origin == "*" {
92 allowAllOrigins = true
93 }
94 if origin != "" {
95 origins.Add(strings.ToLower(origin))
96 }
97 }
98
99 // allow localhost if no allowedOrigins are specified.
100 if len(origins.List()) == 0 {
101 origins.Add("http://localhost")
102 if hostname, err := os.Hostname(); err == nil {
103 origins.Add("http://" + strings.ToLower(hostname))
104 }
105 }
106
107 log.Debug(fmt.Sprintf("Allowed origin(s) for WS RPC interface %v\n", origins.List()))
108
109 f := func(cfg *websocket.Config, req *http.Request) error {
110 origin := strings.ToLower(req.Header.Get("Origin"))
111 if allowAllOrigins || origins.Has(origin) {
112 return nil
113 }
114 log.Warn(fmt.Sprintf("origin '%s' not allowed on WS-RPC interface\n", origin))
115 return fmt.Errorf("origin %s not allowed", origin)
116 }
117
118 return f
119}
120
121// DialWebsocket creates a new RPC client that communicates with a JSON-RPC server
122// that is listening on the given endpoint.

Callers 1

WebsocketHandlerMethod · 0.85

Calls 5

DebugMethod · 0.80
AddMethod · 0.65
GetMethod · 0.65
HasMethod · 0.65
WarnMethod · 0.65

Tested by

no test coverage detected