Skip to content

Commit c39e80f

Browse files
authored
Merge pull request #273 from Julow/improve-api-remote_ip_parsed
Alternative API for `Ocsigen_request.remote_ip`
2 parents 3b7f31c + 3b12a12 commit c39e80f

File tree

6 files changed

+64
-59
lines changed

6 files changed

+64
-59
lines changed

src/extensions/accesscontrol.ml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,19 @@ let ip s =
3939
in
4040
fun ri ->
4141
let r =
42-
match Ocsigen_request.remote_ip_parsed ri with
43-
| `Ip ip -> Ipaddr.Prefix.mem ip prefix
44-
| `Unix _ -> false
42+
match Ocsigen_request.client_conn ri with
43+
| `Inet (ip, _) -> Ipaddr.Prefix.mem ip prefix
44+
| _ -> false
4545
in
4646
if r
4747
then
4848
Logs.info ~src:section (fun fmt ->
49-
fmt "IP: %s matches %s" (Ocsigen_request.remote_ip ri) s)
49+
fmt "IP: %s matches %s" (Ocsigen_request.client_conn_to_string ri) s)
5050
else
5151
Logs.info ~src:section (fun fmt ->
52-
fmt "IP: %s does not match %s" (Ocsigen_request.remote_ip ri) s);
52+
fmt "IP: %s does not match %s"
53+
(Ocsigen_request.client_conn_to_string ri)
54+
s);
5355
r
5456

5557
let port port ri =
@@ -222,22 +224,22 @@ let allow_forward_for_handler ?(check_equal_ip = false) () =
222224
let last_proxy = List.last proxies in
223225
let proxy_ip = Ipaddr.of_string_exn last_proxy in
224226
let equal_ip =
225-
match Ocsigen_request.remote_ip_parsed request_info with
226-
| `Ip r_ip -> Ipaddr.compare proxy_ip r_ip = 0
227-
| `Unix _ -> false
227+
match Ocsigen_request.client_conn request_info with
228+
| `Inet (r_ip, _) -> Ipaddr.compare proxy_ip r_ip = 0
229+
| _ -> false
228230
in
229231
if equal_ip || not check_equal_ip
230232
then
231233
{ request with
232234
Ocsigen_extensions.request_info =
233235
Ocsigen_request.update ~forward_ip:proxies
234-
~remote_ip:original_ip request_info }
236+
~client_conn:(`Forwarded_for original_ip) request_info }
235237
else (
236238
(* the announced ip of the proxy is not its real ip *)
237239
Logs.warn ~src:section (fun fmt ->
238240
fmt
239241
"X-Forwarded-For: host ip (%s) does not match the header (%s)"
240-
(Ocsigen_request.remote_ip request_info)
242+
(Ocsigen_request.client_conn_to_string request_info)
241243
header);
242244
request)
243245
| _ ->

src/extensions/revproxy.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ let gen dir = function
109109
(Ocsigen_request.address request_info)
110110
in
111111
String.concat ", "
112-
(Ocsigen_request.remote_ip request_info
112+
(Ocsigen_request.client_conn_to_string request_info
113113
:: Ocsigen_request.forward_ip request_info
114114
@ [address])
115115
in

src/server/ocsigen_cohttp.ml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,13 @@ end
5757
let handler ~ssl ~address ~port ~connector (flow, conn) request body =
5858
let filenames = ref [] in
5959
let edn = Conduit_lwt_unix.endp_of_flow flow in
60-
let rec getsockname = function
61-
| `TCP (ip, port) -> Unix.ADDR_INET (Ipaddr_unix.to_inet_addr ip, port)
62-
| `Unix_domain_socket path -> Unix.ADDR_UNIX path
63-
| `TLS (_, edn) -> getsockname edn
64-
| `Unknown err -> raise (Failure ("resolution failed: " ^ err))
65-
| `Vchan_direct _ -> raise (Failure "VChan not supported")
66-
| `Vchan_domain_socket _ -> raise (Failure "VChan not supported")
60+
let client_conn =
61+
match edn with
62+
| `TCP (ip, port) | `TLS (_, `TCP (ip, port)) -> `Inet (ip, port)
63+
| `Unix_domain_socket path | `TLS (_, `Unix_domain_socket path) ->
64+
`Unix path
65+
| _ -> `Unknown
6766
in
68-
let sockaddr = getsockname edn in
6967
let connection_closed =
7068
try fst (Hashtbl.find connections conn)
7169
with Not_found ->
@@ -110,7 +108,7 @@ let handler ~ssl ~address ~port ~connector (flow, conn) request body =
110108
in
111109
(* TODO: equivalent of Ocsigen_range *)
112110
let request =
113-
Ocsigen_request.make ~address ~port ~ssl ~filenames ~sockaddr ~body
111+
Ocsigen_request.make ~address ~port ~ssl ~filenames ~client_conn ~body
114112
~connection_closed request
115113
in
116114
Lwt.finalize
@@ -120,7 +118,7 @@ let handler ~ssl ~address ~port ~connector (flow, conn) request body =
120118
(match Ocsigen_request.host request with
121119
| None -> "<host not specified in the request>"
122120
| Some h -> h)
123-
(Ocsigen_request.remote_ip request)
121+
(Ocsigen_request.client_conn_to_string request)
124122
(Option.value ~default:""
125123
(Ocsigen_request.header request Ocsigen_header.Name.user_agent))
126124
(Option.fold ~none:""

src/server/ocsigen_request.ml

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,18 @@ let make_uri u =
4848
and u_get_params_flat = lazy (flatten_get_params (Lazy.force u_get_params)) in
4949
{u_uri; u_get_params; u_get_params_flat; u_path; u_path_string}
5050

51+
type client_conn =
52+
[ `Inet of Ipaddr.t * int
53+
| `Unix of string
54+
| `Forwarded_for of string
55+
| `Unknown ]
56+
5157
type t =
5258
{ r_address : Ocsigen_config.Socket_type.t
5359
; r_port : int
5460
; r_ssl : bool
5561
; r_filenames : string list ref
56-
; r_sockaddr : Lwt_unix.sockaddr
57-
; r_remote_ip : string Lazy.t
58-
; r_remote_ip_parsed : [`Ip of Ipaddr.t | `Unix of string] Lazy.t
62+
; r_client_conn : client_conn
5963
; r_forward_ip : string list
6064
; r_uri : uri
6165
; r_meth : Cohttp.Code.meth
@@ -81,31 +85,16 @@ let make
8185
~port
8286
~ssl
8387
~filenames
84-
~sockaddr
88+
~client_conn
8589
~body
8690
~connection_closed
8791
request
8892
=
89-
let r_remote_ip =
90-
lazy
91-
(match sockaddr with
92-
| Unix.ADDR_INET (ip, _port) -> Unix.string_of_inet_addr ip
93-
| ADDR_UNIX f -> f)
94-
in
95-
let r_remote_ip_parsed =
96-
lazy
97-
(match sockaddr with
98-
| Unix.ADDR_INET (ip, _port) ->
99-
`Ip (Ipaddr.of_string_exn (Unix.string_of_inet_addr ip))
100-
| ADDR_UNIX f -> `Unix f)
101-
in
10293
{ r_address = address
10394
; r_port = port
10495
; r_ssl = ssl
10596
; r_filenames = filenames
106-
; r_sockaddr = sockaddr
107-
; r_remote_ip
108-
; r_remote_ip_parsed
97+
; r_client_conn = client_conn
10998
; r_forward_ip = forward_ip
11099
; r_uri = make_uri (Cohttp.Request.uri request)
111100
; r_encoding = Cohttp.Request.encoding request
@@ -127,7 +116,7 @@ let path {r_uri = {u_path; _}; _} = Lazy.force u_path
127116
let update
128117
?ssl
129118
?forward_ip
130-
?remote_ip
119+
?client_conn
131120
?sub_path
132121
?meth
133122
?get_params_flat
@@ -139,8 +128,7 @@ let update
139128
; r_uri = {u_uri; _} as r_uri
140129
; r_meth
141130
; r_forward_ip
142-
; r_remote_ip
143-
; r_remote_ip_parsed
131+
; r_client_conn
144132
; r_cookies_override
145133
; r_body
146134
; r_sub_path
@@ -150,11 +138,8 @@ let update
150138
let r_ssl = match ssl with Some ssl -> ssl | None -> r_ssl
151139
and r_forward_ip =
152140
match forward_ip with Some forward_ip -> forward_ip | None -> r_forward_ip
153-
and r_remote_ip, r_remote_ip_parsed =
154-
match remote_ip with
155-
| Some remote_ip ->
156-
lazy remote_ip, lazy (`Ip (Ipaddr.of_string_exn remote_ip))
157-
| None -> r_remote_ip, r_remote_ip_parsed
141+
and r_client_conn =
142+
match client_conn with Some c -> c | None -> r_client_conn
158143
and r_sub_path = match sub_path with Some _ -> sub_path | None -> r_sub_path
159144
and r_body =
160145
match post_data with
@@ -192,8 +177,7 @@ let update
192177
; r_uri
193178
; r_meth
194179
; r_forward_ip
195-
; r_remote_ip
196-
; r_remote_ip_parsed
180+
; r_client_conn
197181
; r_body
198182
; r_cookies_override
199183
; r_sub_path
@@ -292,8 +276,15 @@ let post_params r s i =
292276
let files r s i =
293277
match force_post_data r s i with Some v -> Some (v >|= snd) | None -> None
294278

295-
let remote_ip {r_remote_ip; _} = Lazy.force r_remote_ip
296-
let remote_ip_parsed {r_remote_ip_parsed; _} = Lazy.force r_remote_ip_parsed
279+
let client_conn {r_client_conn = c; _} = c
280+
281+
let client_conn_to_string {r_client_conn = c; _} =
282+
match c with
283+
| `Inet (ip, _) -> Ipaddr.to_string ip
284+
| `Unix path -> "unix:" ^ path
285+
| `Forwarded_for ip -> "forwarded:" ^ ip
286+
| `Unknown -> "unknown"
287+
297288
let forward_ip {r_forward_ip; _} = r_forward_ip
298289
let request_cache {r_request_cache; _} = r_request_cache
299290
let tries {r_tries; _} = r_tries

src/server/ocsigen_request.mli

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ type file_info = Ocsigen_multipart.file_info =
99

1010
type post_data = (string * string) list * (string * file_info) list
1111

12+
type client_conn =
13+
[ `Inet of Ipaddr.t * int
14+
| `Unix of string
15+
| `Forwarded_for of string
16+
| `Unknown ]
17+
(** Type of connection used by the client. [`Inet] means the client connected
18+
through the Internet. [`Forwarded_for] means that the client connected
19+
through a proxy and carries the IP address reported in the HTTP headers. *)
20+
1221
val make :
1322
?forward_ip:string list
1423
-> ?sub_path:string
@@ -19,7 +28,7 @@ val make :
1928
-> port:int
2029
-> ssl:bool
2130
-> filenames:string list ref
22-
-> sockaddr:Lwt_unix.sockaddr
31+
-> client_conn:client_conn
2332
-> body:Cohttp_lwt.Body.t
2433
-> connection_closed:unit Lwt.t
2534
-> Cohttp.Request.t
@@ -28,7 +37,7 @@ val make :
2837
val update :
2938
?ssl:bool
3039
-> ?forward_ip:string list
31-
-> ?remote_ip:string
40+
-> ?client_conn:client_conn
3241
-> ?sub_path:string
3342
-> ?meth:Cohttp.Code.meth
3443
-> ?get_params_flat:(string * string) list
@@ -74,8 +83,13 @@ val post_params :
7483
-> Int64.t option
7584
-> (string * string) list Lwt.t option
7685

77-
val remote_ip : t -> string
78-
val remote_ip_parsed : t -> [`Ip of Ipaddr.t | `Unix of string]
86+
val client_conn : t -> client_conn
87+
(** The way the client connects to the server (for example, its IP address if
88+
connected over the internet). *)
89+
90+
val client_conn_to_string : t -> string
91+
(** A textual representation of [client_conn] suitable for use in logs. *)
92+
7993
val forward_ip : t -> string list
8094
val content_type : t -> content_type option
8195
val request_cache : t -> Polytables.t

test/extensions/deflatemod.t/run.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
$ source ../../server-test-helpers.sh
22
$ run_server ./test.exe
33
ocsigen:main: [WARNING] Command pipe created
4-
ocsigen:access: connection for local-test from (): /index.html
4+
ocsigen:access: connection for local-test from unix: (): /index.html
55
ocsigen:ext: [INFO] host found! local-test:0 matches .*
66
ocsigen:ext:staticmod: [INFO] Is it a static file?
77
ocsigen:local-file: [INFO] Testing "./index.html".
88
ocsigen:local-file: [INFO] checking if file index.html can be sent
99
ocsigen:ext: [INFO] Compiling exclusion regexp $^
1010
ocsigen:local-file: [INFO] Returning "./index.html".
11-
ocsigen:access: connection for local-test from (): /index.html
11+
ocsigen:access: connection for local-test from unix: (): /index.html
1212
ocsigen:ext: [INFO] host found! local-test:0 matches .*
1313
ocsigen:ext:staticmod: [INFO] Is it a static file?
1414
ocsigen:local-file: [INFO] Testing "./index.html".

0 commit comments

Comments
 (0)