Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add websocket_origin parameters to customize Origin header #388

Merged
merged 1 commit into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/ts_profile.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
ip_transparent = false, % set IP_TRANSPARENT option on the socket
websocket_path = "/chat", % for websocket only
websocket_frame = "binary", % for websocket only
websocket_origin = "", % for websocket only
websocket_subprotocols = [], % for websocket only
retry_timeout = 10, % retry sending in milliseconds
max_retries = 3, % maximum number of retries
Expand Down Expand Up @@ -96,6 +97,7 @@
retries=0, % number of connect retries
hibernate = 10000, % hibernate if thinktime is >= to this (10sec by default)
host, % hostname (or IP) of remote server
origin, % Origin Header
port, % server port
protocol, % gen_udp, gen_tcp or ssl
proto_opts = #proto_opts{}, %
Expand Down
14 changes: 7 additions & 7 deletions src/tsung/ts_server_websocket_ssl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
-include("ts_config.hrl").
-include("ts_websocket.hrl").

-record(state, {parent, socket = none, accept, host, port, path, opts, version,
-record(state, {parent, socket = none, accept, host, port, path, opts, version, origin,
frame, buffer = <<>>, state = not_connected, subprotos = []}).

-record(ws_config, {path, version = "13", frame, subprotos}).
-record(ws_config, {path, version = "13", frame, origin, subprotos}).

protocol_options(#proto_opts{tcp_rcv_size = Rcv, tcp_snd_size = Snd,
websocket_path = Path, websocket_frame = Frame, websocket_subprotocols = SubProtocols}) ->
[#ws_config{path = Path, frame = Frame, subprotos = SubProtocols},
websocket_path = Path, websocket_frame = Frame, websocket_origin = Origin, websocket_subprotocols = SubProtocols}) ->
[#ws_config{path = Path, frame = Frame, origin = Origin, subprotos = SubProtocols},
binary,
{active, once},
{recbuf, Rcv},
Expand All @@ -58,12 +58,13 @@ connect(Host, Port, Opts, Timeout) ->
Version = WSConfig#ws_config.version,
Frame = WSConfig#ws_config.frame,
Protocol = WSConfig#ws_config.subprotos,
Origin = WSConfig#ws_config.origin,

case ssl:connect(Host, Port, opts_to_tcp_opts(TcpOpts),Timeout) of
{ok, Socket} ->
Pid = spawn_link(
fun() ->
loop(#state{parent = Parent, host = Host, port = Port, subprotos = Protocol,
loop(#state{parent = Parent, host = Host, port = Port, subprotos = Protocol, origin = Origin,
opts = TcpOpts, path = Path, version = Version,
frame = Frame, socket = Socket})
end),
Expand All @@ -75,9 +76,8 @@ connect(Host, Port, Opts, Timeout) ->
end.

loop(#state{socket = Socket, host = Host, path = Path,
version = Version, subprotos = SubProtocol,
version = Version, subprotos = SubProtocol, origin = Origin,
state = not_connected} = State)->
Origin = "",
Headers = "",
{Handshake, Accept} = websocket:get_handshake(Host, Path, SubProtocol, Version, Origin, Headers),
ssl:send(Socket, Handshake),
Expand Down
6 changes: 6 additions & 0 deletions src/tsung_controller/ts_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,12 @@ parse(Element = #xmlElement{name=option, attributes=Attrs},
NewProto = OldProto#proto_opts{websocket_subprotocols=SubProtocols},
lists:foldl( fun parse/2, Conf#config{proto_opts=NewProto},
Element#xmlElement.content);
"websocket_origin" ->
Origin = getAttr(string,Attrs, value, ?config(websocket_origin)),
OldProto = Conf#config.proto_opts,
NewProto = OldProto#proto_opts{websocket_origin=Origin},
lists:foldl( fun parse/2, Conf#config{proto_opts=NewProto},
Element#xmlElement.content);
"bosh_path" ->
Path = getAttr(string,Attrs, value, ?config(bosh_path)),
OldProto = Conf#config.proto_opts,
Expand Down