-
Notifications
You must be signed in to change notification settings - Fork 599
/
haproxy.inc
3158 lines (2880 loc) · 123 KB
/
haproxy.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* haproxy.inc
*
* part of pfSense (https://www.pfsense.org)
* Copyright (c) 2009-2024 Rubicon Communications, LLC (Netgate)
* Copyright (c) 2013-2016 PiBa-NL
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* include all configuration functions */
require_once("functions.inc");
require_once("pkg-utils.inc");
require_once("notices.inc");
require_once("filter.inc");
require_once("haproxy_utils.inc");
require_once("haproxy_socketinfo.inc");
require_once("haproxy_xmlrpcsyncclient.inc");
$d_haproxyconfdirty_path = $g['varrun_path'] . "/haproxy.conf.dirty";
#region Global haproxy array item definitions..
global $a_frontendmode;
$a_frontendmode = array();
$a_frontendmode['http'] = array('name' => "http / https(offloading)", 'shortname' => "http/https");
$a_frontendmode['https'] = array('name' => "ssl / https(TCP mode)", 'shortname' => "ssl/https");
$a_frontendmode['tcp'] = array('name' => "tcp", 'shortname' => "tcp");
/*
if anyone actually used this what was it good for with recent package versions?
$a_frontendmode['health'] = array('name' => "health", 'shortname' => "health");
*/
global $openssl111;
$openssl111 = (OPENSSL_VERSION_NUMBER >= 269488128);
global $a_acltypes;
$a_acltypes = array();
$a_acltypes["host_starts_with"] = array('name' => 'Host starts with:', 'casesensitive' => true,
'mode' => 'http', 'syntax' => 'var(txn.txnhost) -m beg{casesensitive} %1$s', 'advancedoptions' => "http-request set-var(txn.txnhost) hdr(host)");
$a_acltypes["host_ends_with"] = array('name' => 'Host ends with:', 'casesensitive' => true,
'mode' =>'http', 'syntax' => 'var(txn.txnhost) -m end{casesensitive} %1$s', 'advancedoptions' => "http-request set-var(txn.txnhost) hdr(host)");
$a_acltypes["host_matches"] = array('name' => 'Host matches:', 'casesensitive' => true,
'mode' =>'http', 'syntax' => 'var(txn.txnhost) -m str{casesensitive} %1$s', 'advancedoptions' => "http-request set-var(txn.txnhost) hdr(host)");
$a_acltypes["host_regex"] = array('name' => 'Host regex:', 'casesensitive' => true,
'mode' =>'http', 'syntax' => 'var(txn.txnhost) -m reg{casesensitive} %1$s', 'advancedoptions' => "http-request set-var(txn.txnhost) hdr(host)");
$a_acltypes["host_contains"] = array('name' => 'Host contains:', 'casesensitive' => true,
'mode' => 'http', 'syntax' => 'var(txn.txnhost) -m sub{casesensitive} %1$s', 'advancedoptions' => "http-request set-var(txn.txnhost) hdr(host)");
$a_acltypes["path_starts_with"] = array('name' => 'Path starts with:', 'casesensitive' => true,
'mode' => 'http', 'syntax' => 'var(txn.txnpath) -m beg{casesensitive} %1$s', 'advancedoptions' => "http-request set-var(txn.txnpath) path");
$a_acltypes["path_ends_with"] = array('name' => 'Path ends with:', 'casesensitive' => true,
'mode' => 'http', 'syntax' => 'var(txn.txnpath) -m end{casesensitive} %1$s', 'advancedoptions' => "http-request set-var(txn.txnpath) path");
$a_acltypes["path_matches"] = array('name' => 'Path matches:', 'casesensitive' => true,
'mode' => 'http', 'syntax' => 'var(txn.txnpath) -m str{casesensitive} %1$s', 'advancedoptions' => "http-request set-var(txn.txnpath) path");
$a_acltypes["path_regex"] = array('name' => 'Path regex:', 'casesensitive' => true,
'mode' => 'http', 'syntax' => 'var(txn.txnpath) -m reg{casesensitive} %1$s', 'advancedoptions' => "http-request set-var(txn.txnpath) path");
$a_acltypes["path_contains"] = array('name' => 'Path contains:', 'casesensitive' => true,
'mode' => 'http', 'syntax' => 'var(txn.txnpath) -m sub{casesensitive} %1$s', 'advancedoptions' => "http-request set-var(txn.txnpath) path");
$a_acltypes["path_dir"] = array('name' => 'Path contains within slashes:', 'casesensitive' => true,
'mode' => 'http', 'syntax' => 'var(txn.txnpath) -m dir{casesensitive} %1$s', 'advancedoptions' => "http-request set-var(txn.txnpath) path");
$a_acltypes["url_parameter"] = array('name' => 'Url parameter contains:', 'casesensitive' => true,
'mode' => 'http', 'syntax' => 'url_param({parameter}){casesensitive} %1$s',
'fields' => array(
array('name'=>"parameter",'columnheader'=>"Parameter name",'type'=>"textbox",'size'=>"50",'mask'=>'urlparameter')
));
$a_acltypes["ssl_c_verify_code"] = array('name' => 'SSL Client certificate verify error result:',
'mode' => 'http', 'syntax' => 'ssl_c_verify %1$s', 'require_client_cert' => '1');
// ssl_c_verify result codes: https://www.openssl.org/docs/apps/verify.html#DIAGNOSTICS
$a_acltypes["ssl_c_verify"] = array('name' => 'SSL Client certificate valid.',
'mode' => 'http', 'syntax' => 'ssl_c_verify 0', 'novalue' => '1', 'require_client_cert' => '1');
$a_acltypes["ssl_c_ca_commonname"] = array('name' => 'SSL Client issued by CA common-name:',
'mode' => 'http', 'syntax' => 'ssl_c_i_dn(CN) %1$s', 'require_client_cert' => '1');
$a_acltypes["source_ip"] = array('name' => 'Source IP matches IP or Alias:',
'mode' => '', 'syntax' => 'src %1$s');
$a_acltypes["backendservercount"] = array('name' => 'Minimum count usable servers:',
'mode' => '', 'syntax' => 'nbsrv({backend}) ge %1$d', 'parameters' => 'value,backendname',
'fields' => array(
'backend' => array('name'=>"backend",'columnheader'=>"Backend",'type'=>"select",'size'=>"50",'mask'=>'backend')
));
$a_acltypes["traffic_is_http"] = array('name' => 'Traffic is http (no value needed):', 'inspect-delay' => '5',
'mode' => 'tcp', 'syntax' => 'req.proto_http', 'advancedoptions' => "tcp-request content accept if { req.proto_http }");
$a_acltypes["traffic_is_ssl"] = array('name' => 'Traffic is ssl (no value needed):', 'inspect-delay' => '5',
'mode' => 'tcp', 'syntax' => 'req.ssl_ver gt 0', 'advancedoptions' => "tcp-request content accept if { req.ssl_ver gt 0 }");
// 'ssl_sni_matches' was added in HAProxy1.5dev17
$a_acltypes["ssl_sni_matches"] = array('name' => 'Server Name Indication TLS extension matches:', 'inspect-delay' => '5', 'casesensitive' => true,
'mode' => 'https', 'syntax' => 'req.ssl_sni{casesensitive} %1$s', 'advancedoptions' => "tcp-request content accept if { req.ssl_hello_type 1 }");
$a_acltypes["ssl_sni_contains"] = array('name' => 'Server Name Indication TLS extension contains:', 'inspect-delay' => '5', 'casesensitive' => true,
'mode' => 'https', 'syntax' => 'req.ssl_sni -m sub{casesensitive} %1$s', 'advancedoptions' => "tcp-request content accept if { req.ssl_hello_type 1 }");
$a_acltypes["ssl_sni_starts_with"] = array('name' => 'Server Name Indication TLS extension starts with:', 'inspect-delay' => '5', 'casesensitive' => true,
'mode' => 'https', 'syntax' => 'req.ssl_sni -m beg{casesensitive} %1$s', 'advancedoptions' => "tcp-request content accept if { req.ssl_hello_type 1 }");
$a_acltypes["ssl_sni_ends_with"] = array('name' => 'Server Name Indication TLS extension ends with:', 'inspect-delay' => '5', 'casesensitive' => true,
'mode' => 'https', 'syntax' => 'req.ssl_sni -m end{casesensitive} %1$s', 'advancedoptions' => "tcp-request content accept if { req.ssl_hello_type 1 }");
$a_acltypes["ssl_sni_regex"] = array('name' => 'Server Name Indication TLS extension regex:', 'inspect-delay' => '5', 'casesensitive' => true,
'mode' => 'https', 'syntax' => 'req.ssl_sni -m reg{casesensitive} %1$s', 'advancedoptions' => "tcp-request content accept if { req.ssl_hello_type 1 }");
$a_acltypes["custom"] = array('name' => 'Custom acl:',
'mode' => '', 'syntax' => '%1$s');
global $a_checktypes;
$a_checktypes = array();
$a_checktypes['none'] = array('name' => 'none', 'syntax' => '',
'descr' => 'No health checks will be performed.');
$a_checktypes['Basic'] = array('name' => 'Basic', 'syntax' => '',
'descr' => 'Basic socket connection check');
$a_checktypes['HTTP'] = array('name' => 'HTTP', 'syntax' => 'httpchk',
'descr' => 'HTTP protocol to check on the servers health, can also be used for HTTPS servers(requirs checking the SSL box for the servers).', 'parameters' => "uri,method,version");
// 'Agent' was added in HAProxy1.5dev18, and removed in 1.5dev20, in favor of the seperate agent-check option.
$a_checktypes['Agent'] = array('name' => 'Agent', 'syntax' => 'lb-agent-chk', 'usedifferenport' => 'yes',
'descr' => 'Use a TCP connection to read an ASCII string of the form 100%,75%,drain,down (others in haproxy manual)',
'deprecated' => true);
$a_checktypes['LDAP'] = array('name' => 'LDAP', 'syntax' => 'ldap-check',
'descr' => 'Use LDAPv3 health checks for server testing');
$a_checktypes['MySQL'] = array('name' => 'MySQL', 'syntax' => 'mysql-check',
'descr' => 'Use MySQL health checks for server testing', 'parameters' => 'username');
$a_checktypes['PostgreSQL'] = array('name' => 'PostgreSQL', 'syntax' => 'pgsql-check',
'descr' => 'Use PostgreSQL health checks for server testing', 'parameters' => 'username');
$a_checktypes['Redis'] = array('name' => 'Redis', 'syntax' => 'redis-check',
'descr' => 'Test that the server correctly talks REDIS protocol.');
$a_checktypes['SMTP'] = array('name' => 'SMTP', 'syntax' => 'smtpchk HELO',
'descr' => 'Use SMTP HELO health checks for server testing', 'parameters' => 'domain');
$a_checktypes['ESMTP'] = array('name' => 'ESMTP', 'syntax' => 'smtpchk EHLO',
'descr' => 'Use ESMTP EHLO health checks for server testing', 'parameters' => 'domain');
$a_checktypes['SSL'] = array('name' => 'SSL', 'syntax' => 'ssl-hello-chk',
'descr' => 'Use SSLv3 client hello health checks for server testing.');
global $a_httpcheck_method;
$a_httpcheck_method = array();
$a_httpcheck_method['OPTIONS'] = array('name' => 'OPTIONS', 'syntax' => 'OPTIONS');
$a_httpcheck_method['HEAD'] = array('name' => 'HEAD', 'syntax' => 'HEAD');
$a_httpcheck_method['GET'] = array('name' => 'GET', 'syntax' => 'GET');
$a_httpcheck_method['POST'] = array('name' => 'POST', 'syntax' => 'POST');
$a_httpcheck_method['PUT'] = array('name' => 'PUT', 'syntax' => 'PUT');
$a_httpcheck_method['DELETE'] = array('name' => 'DELETE', 'syntax' => 'DELETE');
$a_httpcheck_method['TRACE'] = array('name' => 'TRACE', 'syntax' => 'TRACE');
global $a_closetypes;
$a_closetypes = array();
//$a_closetypes['none'] = array('name' => 'none', 'syntax' => '',
// 'descr' => 'No close headers will be changed.');
$a_closetypes['http-keep-alive'] = array('name' => 'http-keep-alive (default)', 'syntax' => 'http-keep-alive',
'descr' => 'By default HAProxy operates in keep-alive mode with regards to persistent connections: for each connection it processes each request and response, and leaves the connection idle on both sides between the end of a response and the start of a new request.');
$a_closetypes['http-tunnel'] = array('name' => 'http-tunnel', 'syntax' => 'http-tunnel',
'descr' => 'Option "http-tunnel" disables any HTTP processing past the first request and the first response. This is the mode which was used by default in versions 1.0 to 1.5-dev21. It is the mode with the lowest processing overhead, which is normally not needed anymore unless in very specific cases such as when using an in-house protocol that looks like HTTP but is not compatible, or just to log one request per client in order to reduce log size. Note that everything which works at the HTTP level, including header parsing/addition, cookie processing or content switching will only work for the first request and will be ignored after the first response.');
$a_closetypes['httpclose'] = array('name' => 'httpclose', 'syntax' => 'httpclose',
'descr' => 'The "httpclose" option removes any "Connection" header both ways, and adds a "Connection: close" header in each direction. This makes it easier to disable HTTP keep-alive than the previous 4-rules block.');
$a_closetypes['http-server-close'] = array('name' => 'http-server-close', 'syntax' => 'http-server-close',
'descr' => 'By default, when a client communicates with a server, HAProxy will only analyze, log, and process the first request of each connection. Setting "option http-server-close" enables HTTP connection-close mode on the server side while keeping the ability to support HTTP keep-alive and pipelining on the client side. This provides the lowest latency on the client side (slow network) and the fastest session reuse on the server side to save server resources.');
$a_closetypes['forceclose'] = array('name' => 'forceclose', 'syntax' => 'forceclose',
'descr' => 'Some HTTP servers do not necessarily close the connections when they receive the "Connection: close" set by "option httpclose", and if the client does not close either, then the connection remains open till the timeout expires. This causes high number of simultaneous connections on the servers and shows high global session times in the logs. Note that this option also enables the parsing of the full request and response, which means we can close the connection to the server very quickly, releasing some resources earlier than with httpclose.');
global $a_servermodes;
$a_servermodes = array();
$a_servermodes["active"]['name'] = "active";
$a_servermodes["active"]['sign'] = "";
$a_servermodes["backup"]['name'] = "backup";
$a_servermodes["backup"]['sign'] = "*";
$a_servermodes["disabled"]['name'] = "disabled";
$a_servermodes["disabled"]['sign'] = "?";
$a_servermodes["inactive"]['name'] = "inactive";
$a_servermodes["inactive"]['sign'] = "-";
// http://www.exceliance.fr/sites/default/files/biblio/aloha_load_balancer_haproxy_cookie_persistence_methods_memo.pdf
global $a_cookiemode;
$a_cookiemode = array();
$a_cookiemode['passive'] = array('name' => 'Passive', 'syntax' => 'cookie <cookie name>',
'descr' => 'Cookie is analysed on incoming request to choose server. HAProxy does not perform any insertion update or deletion on the Cookie or Set-Cookie. If the Cookie is not set, then the load-balancing algorithm is applied.');
$a_cookiemode['passive-silent'] = array('name' => 'Passive-silent', 'syntax' => 'cookie <cookie name> indirect',
'descr' => 'Cookie is analysed on incoming request to choose server. HAProxy does not perform any insertion, update or deletion on the Cookie. Set-Cookie is removed from response if not required. If the Cookie is not set, then HAProxy applies the load-balancing algorithm.');
$a_cookiemode['reset'] = array('name' => 'Reset', 'syntax' => 'cookie <cookie name> rewrite',
'descr' => 'Cookie is analysed on incoming request to choose server and Set-Cookie value is overwritten in response if present. If the Set-Cookie isn\'t sent by the server, then HAProxy won\'t set it.');
$a_cookiemode['set'] = array('name' => 'Insert', 'syntax' => 'cookie <cookie name> insert',
'descr' => 'Cookie is analyzed on incoming request to choose server and Set-Cookie value is overwritten if present and set to an unknown value or inserted in response if not present.');
$a_cookiemode['set-silent'] = array('name' => 'Insert-silent', 'syntax' => 'cookie <cookie name> insert indirect',
'descr' => 'Cookie is analyzed on incoming request to choose server and Set-Cookie value is overwritten if present, inserted in response if needed and removed if a valid Cookie was provided.');
$a_cookiemode['insert-only'] = array('name' => 'Insert-preserve', 'syntax' => 'cookie <cookie name> preserve insert',
'descr' => 'Cookie is analyzed on incoming request to choose server. Set-Cookie value is set only if the server does not provide one or if the client came without the Cookie.');
$a_cookiemode['insert-only-silent'] = array('name' => 'Insert-preserve-silent', 'syntax' => 'cookie <cookie name> preserve insert indirect',
'descr' => 'Cookie is analyzed on incoming request to choose server and Set-Cookie value is left untouched if present, inserted in response if needed or removed if not needed.');
$a_cookiemode['session-prefix'] = array('name' => 'Session-prefix', 'syntax' => 'cookie <cookie name> prefix',
'descr' => 'Cookie is analyzed on incoming request to choose server whose Cookie Name prefix matches. Set Cookie value is prefixed using server line Cookie ID in response. Cookie is modified only between HAProxy and the client only');
$a_cookiemode['passive-session-prefix'] = array('name' => 'Passive-session-prefix', 'syntax' => 'cookie <cookie name> preserve prefix indirect',
'descr' => 'Cookie is analysed on incoming request to choose server whose Cookie ID prefix matches.');
foreach($a_cookiemode as &$cookiemode) {
$cookiemode['descr'] = $cookiemode['descr'] . "\n\n" . $cookiemode['syntax'] . "";
}
global $a_sticky_type;
$a_sticky_type = array();
$a_sticky_type['none'] = array('name' => 'none',
'descr' => "No stick-table will be used");
$a_sticky_type['stick_sslsessionid'] = array('name' => 'Stick on SSL-Session-ID',
'descr' => "Only used on https frontends. Uses the SSL-Session-ID to persist clients to a server.");
$a_sticky_type['stick_sourceipv4'] = array('name' => 'Stick on SourceIP IPv4',
'descr' => "Stick on the client ip, drawback is that multiple clients behind a natted public ip will be balanced to the same server.");
$a_sticky_type['stick_sourceipv6'] = array('name' => 'Stick on SourceIP IPv6',
'descr' => "Stick on the client ip, drawback is that multiple clients behind a natted public ip will be balanced to the same server.");
$a_sticky_type['stick_cookie_value'] = array('name' => 'Stick on existing Cookie value',
'descr' => "Stick on the value of a session cookie",
'cookiedescr' => "Enables SSL-session-id based persistence. (only use on 'https' and 'tcp' frontends that use SSL)<br/>EXAMPLE: JSESSIONID PHPSESSIONID ASP.NET_SessionId");
$a_sticky_type['stick_rdp_cookie'] = array('name' => 'Stick on RDP-cookie',
'descr' => "Uses a RDP-Cookie send by the mstsc client, note that not all clients send this.",
'cookiedescr' => 'EXAMPLE: msts or mstshash');
global $a_error;
$a_error = array();
$a_error['200'] = array('descr' => "stats or monitoring requests");
$a_error['400'] = array('descr' => "request invalid or too large");
$a_error['401'] = array('descr' => "authentication is required to perform the action");
$a_error['403'] = array('descr' => "request is forbidden");
$a_error['408'] = array('descr' => "timeout before the request is complete");
$a_error['500'] = array('descr' => "internal error");
$a_error['502'] = array('descr' => "server response invalid or blocked");
$a_error['503'] = array('descr' => "no server was available to handle the request");
$a_error['504'] = array('descr' => "timeout before the server responds");
global $a_sysloglevel;
$a_sysloglevel = array();
$a_sysloglevel['emerg'] = array('name' => "Emergency");
$a_sysloglevel['alert'] = array('name' => "Alert");
$a_sysloglevel['crit'] = array('name' => "Critical");
$a_sysloglevel['err'] = array('name' => "Error");
$a_sysloglevel['warning'] = array('name' => "Warning");
$a_sysloglevel['notice'] = array('name' => "Notice");
$a_sysloglevel['info'] = array('name' => "Informational");
$a_sysloglevel['debug'] = array('name' => "Debugging");
global $a_facilities;
$a_facilities = array();
$facilities = array("kern", "user", "mail", "daemon", "auth", "syslog", "lpr",
"news", "uucp", "cron", "auth2", "ftp", "ntp", "audit", "alert", "cron2",
"local0", "local1", "local2", "local3", "local4", "local5", "local6", "local7");
foreach($facilities as $facility) {
$a_facilities[$facility] = array('name' => $facility);
}
global $a_filestype;
$a_filestype = array();
$a_filestype[''] = array('name' => "Errorfile");
$a_filestype['luascript'] = array('name' => "Lua script");
$a_filestype['writetodisk'] = array('name' => "Write to disk");
global $a_action;
$a_action = array();
//
$a_action["use_backend"] = array('name' => "Use Backend", 'mode' => '', 'syntax' => 'use_backend {backend}', 'usage' => 'frontend',
'fields' => array(
'backend' => array('name'=>"backend",'columnheader'=>"Backend",'type'=>"select",'size'=>"50",'mask'=>'backend')
));
$a_action["use_server"] = array('name' => "Use Server", 'mode' => '', 'syntax' => 'use-server {server}', 'usage' => 'backend',
'fields' => array(
'server' => array('name'=>"server",'columnheader'=>"Server",'type'=>"textbox",'size'=>"50",'mask'=>'server')
));
//
$a_action["custom"] = array('name' => "Custom", 'mode' => '', 'syntax' => '{customaction}',
'fields' => array(
array('name'=>"customaction",'columnheader'=>"Custom action",'type'=>"textbox",'size'=>"50",'mask'=>'freetext')
));
//
$a_action["http-request_allow"] = array('name' => "http-request allow", 'mode'=> 'http', 'syntax' => 'http-request allow');
$a_action["http-request_deny"] = array('name' => "http-request deny", 'mode'=> 'http', 'syntax' => 'http-request deny {deny_status}',
'fields' => array(
array('name'=>"deny_status",'columnheader'=>"Status code",'type'=>"textbox",'size'=>"50",'mask'=>'text','prefix'=>'deny_status')
));
$a_action["http-request_tarpit"] = array('name' => "http-request tarpit", 'mode'=> 'http', 'syntax' => 'http-request tarpit {deny_status}',
'fields' => array(
array('name'=>"deny_status",'columnheader'=>"Status code",'type'=>"textbox",'size'=>"50",'mask'=>'text','prefix'=>'deny_status')
));
$a_action["http-request_auth"] = array('name' => "http-request auth", 'mode'=> 'http', 'syntax' => 'http-request auth {realm}',
'fields' => array(
array('name'=>"realm",'columnheader'=>"Realm",'type'=>"textbox",'size'=>"50",'mask'=>'freetext')
)
);
$a_action["http-request_redirect"] = array('name' => "http-request redirect", 'mode'=> 'http', 'syntax' => 'http-request redirect {rule}',
'fields' => array(
array('name'=>"rule",'columnheader'=>"Rule",'type'=>"textbox",'size'=>"50",'mask'=>'logformat')
)
);
if (haproxy_version() >= '1.6') {
$a_action["http-request_lua"] = array('name' => "http-request lua action", 'mode'=> 'http', 'syntax' => 'http-request lua.{lua-function}',
'fields' => array(
'lua-function' => array('name'=>"lua-function",'columnheader'=>"lua function",'type'=>"textbox",'size'=>"50",'mask'=>'lua-function')
));
$a_action["http-request_use-service"] = array('name' => "http-request lua service", 'mode'=> 'http', 'syntax' => 'http-request use-service lua.{lua-function}',
'fields' => array(
'lua-function' => array('name'=>"lua-function",'columnheader'=>"lua function",'type'=>"textbox",'size'=>"50",'mask'=>'lua-function')
));
}
$a_action["http-request_add-header"] = array('name' => "http-request header add", 'mode'=> 'http', 'syntax' => 'http-request add-header {name} {fmt}',
'fields' => array(
array('name'=>"name",'columnheader'=>"Headername",'type'=>"textbox",'size'=>"50",'mask'=>'headername'),
array('name'=>"fmt",'columnheader'=>"New logformat value",'type'=>"textbox",'size'=>"50",'mask'=>'logformat')
));
$a_action["http-request_set-header"] = array('name' => "http-request header set", 'mode'=> 'http', 'syntax' => 'http-request set-header {name} {fmt}',
'fields' => array(
array('name'=>"name",'columnheader'=>"Headername",'type'=>"textbox",'size'=>"50",'mask'=>'headername'),
array('name'=>"fmt",'columnheader'=>"New logformat value",'type'=>"textbox",'size'=>"50",'mask'=>'logformat')
));
$a_action["http-request_del-header"] = array('name' => "http-request header delete", 'mode'=> 'http', 'syntax' => 'http-request del-header {name}',
'fields' => array(
array('name'=>"name",'columnheader'=>"Headername",'type'=>"textbox",'size'=>"50",'mask'=>'headername')
));
$a_action["http-request_replace-header"] = array('name' => "http-request header replace", 'mode'=> 'http', 'syntax' => 'http-request replace-header {name} {find} {replace}',
'fields' => array(
array('name'=>"name",'columnheader'=>"Headername",'type'=>"textbox",'size'=>"50",'mask'=>'headername'),
array('name'=>"find",'columnheader'=>"Find regex",'type'=>"textbox",'size'=>"50",'mask'=>'match-regex'),
array('name'=>"replace",'columnheader'=>"Replace by",'type'=>"textbox",'size'=>"50",'mask'=>'replace-fmt')
));
if (haproxy_version() >= '2.2') {
$a_action["http-request_replace-path"] = array('name' => "http-request header replace path", 'mode'=> 'http', 'syntax' => 'http-request replace-path {find} {path} {replace}',
'fields' => array(
array('name'=>"path",'columnheader'=>"Path",'type'=>"textbox",'size'=>"50",'mask'=>'headername'),
array('name'=>"find",'columnheader'=>"Find regex",'type'=>"textbox",'size'=>"50",'mask'=>'match-regex'),
array('name'=>"replace",'columnheader'=>"Replace by",'type'=>"textbox",'size'=>"50",'mask'=>'replace-fmt')
));
}
$a_action["http-request_replace-value"] = array('name' => "http-request header replace value", 'mode'=> 'http', 'syntax' => 'http-request replace-value {name} {find} {replace}',
'fields' => array(
array('name'=>"name",'columnheader'=>"Headername",'type'=>"textbox",'size'=>"50",'mask'=>'headername'),
array('name'=>"find",'columnheader'=>"Find regex",'type'=>"textbox",'size'=>"50",'mask'=>'match-regex'),
array('name'=>"replace",'columnheader'=>"Replace by",'type'=>"textbox",'size'=>"50",'mask'=>'replace-fmt')
));
$a_action["http-request_set-method"] = array('name' => "http-request set method", 'mode'=> 'http', 'syntax' => 'http-request set-method {fmt}',
'fields' => array(
array('name'=>"fmt",'columnheader'=>"New logformat value",'type'=>"textbox",'size'=>"50",'mask'=>'logformat')
));
$a_action["http-request_set-path"] = array('name' => "http-request set path", 'mode'=> 'http', 'syntax' => 'http-request set-path {fmt}',
'fields' => array(
array('name'=>"fmt",'columnheader'=>"New logformat value",'type'=>"textbox",'size'=>"50",'mask'=>'logformat')
));
$a_action["http-request_set-query"] = array('name' => "http-request set query", 'mode'=> 'http', 'syntax' => 'http-request set-query {fmt}',
'fields' => array(
array('name'=>"fmt",'columnheader'=>"New logformat value",'type'=>"textbox",'size'=>"50",'mask'=>'logformat')
));
$a_action["http-request_set-uri"] = array('name' => "http-request set uri", 'mode'=> 'http', 'syntax' => 'http-request set-uri {fmt}',
'fields' => array(
array('name'=>"fmt",'columnheader'=>"New logformat value",'type'=>"textbox",'size'=>"50",'mask'=>'logformat')
));
//
$a_action["http-response_allow"] = array('name' => "http-response allow", 'mode'=> 'http', 'syntax' => 'http-response allow');
$a_action["http-response_deny"] = array('name' => "http-response deny", 'mode'=> 'http', 'syntax' => 'http-response deny');
if (haproxy_version() >= '1.6') {
$a_action["http-response_lua"] = array('name' => "http-response lua script", 'mode'=> 'http', 'syntax' => 'http-response lua.{lua-function}',
'fields' => array(
'lua-function' => array('name'=>"lua-function",'columnheader'=>"lua function",'type'=>"textbox",'size'=>"50",'mask'=>'lua-function')
));
}
$a_action["http-response_add-header"] = array('name' => "http-response header add", 'mode'=> 'http', 'syntax' => 'http-response add-header {name} {fmt}',
'fields' => array(
array('name'=>"name",'columnheader'=>"Headername",'type'=>"textbox",'size'=>"50",'mask'=>'headername'),
array('name'=>"fmt",'columnheader'=>"New logformat value",'type'=>"textbox",'size'=>"50",'mask'=>'logformat')
));
$a_action["http-response_set-header"] = array('name' => "http-response header set", 'mode'=> 'http', 'syntax' => 'http-response set-header {name} {fmt}',
'fields' => array(
array('name'=>"name",'columnheader'=>"Headername",'type'=>"textbox",'size'=>"50",'mask'=>'headername'),
array('name'=>"fmt",'columnheader'=>"New logformat value",'type'=>"textbox",'size'=>"50",'mask'=>'logformat')
));
$a_action["http-response_del-header"] = array('name' => "http-response header delete", 'mode'=> 'http', 'syntax' => 'http-response del-header {name}',
'fields' => array(
array('name'=>"name",'columnheader'=>"Headername",'type'=>"textbox",'size'=>"50",'mask'=>'headername')
));
$a_action["http-response_replace-header"] = array('name' => "http-response header replace", 'mode'=> 'http', 'syntax' => 'http-response replace-header {name} {find} {replace}',
'fields' => array(
array('name'=>"name",'columnheader'=>"Headername",'type'=>"textbox",'size'=>"50",'mask'=>'headername'),
array('name'=>"find",'columnheader'=>"Find regex",'type'=>"textbox",'size'=>"50",'mask'=>'match-regex'),
array('name'=>"replace",'columnheader'=>"Replace by",'type'=>"textbox",'size'=>"50",'mask'=>'replace-fmt')
));
$a_action["http-response_replace-value"] = array('name' => "http-response header replace value", 'mode'=> 'http', 'syntax' => 'http-response replace-value {name} {find} {replace}',
'fields' => array(
array('name'=>"name",'columnheader'=>"Headername",'type'=>"textbox",'size'=>"50",'mask'=>'headername'),
array('name'=>"find",'columnheader'=>"Find regex",'type'=>"textbox",'size'=>"50",'mask'=>'match-regex'),
array('name'=>"replace",'columnheader'=>"Replace by",'type'=>"textbox",'size'=>"50",'mask'=>'replace-fmt')
));
$a_action["http-response_set-status"] = array('name' => "http-response set status", 'mode'=> 'http', 'syntax' => 'http-response set-status {status} {reason}',
'fields' => array(
array('name'=>"status",'columnheader'=>"Status",'type'=>"textbox",'size'=>"50",'mask'=>'text'),
array('name'=>"reason",'columnheader'=>"Reason",'type'=>"textbox",'size'=>"50",'mask'=>'text','prefix'=>'reason')
));
if (haproxy_version() >= '2.2') {
$a_action["http-after-response_add-header"] = array('name' => "http-after-response header add", 'mode'=> 'http', 'syntax' => ' http-after-response add-header {name} {fmt}',
'fields' => array(
array('name'=>"name",'columnheader'=>"Headername",'type'=>"textbox",'size'=>"50",'mask'=>'headername'),
array('name'=>"fmt",'columnheader'=>"New logformat value",'type'=>"textbox",'size'=>"50",'mask'=>'logformat')
));
$a_action["http-after-response_set-header"] = array('name' => "http-after-response header set", 'mode'=> 'http', 'syntax' => 'http-after-response set-header {name} {fmt}',
'fields' => array(
array('name'=>"name",'columnheader'=>"Headername",'type'=>"textbox",'size'=>"50",'mask'=>'headername'),
array('name'=>"fmt",'columnheader'=>"New logformat value",'type'=>"textbox",'size'=>"50",'mask'=>'logformat')
));
$a_action["http-after-response_del-header"] = array('name' => "http-after-response header delete", 'mode'=> 'http', 'syntax' => 'http-after-response del-header {name}',
'fields' => array(
array('name'=>"name",'columnheader'=>"Headername",'type'=>"textbox",'size'=>"50",'mask'=>'headername')
));
$a_action["http-after-response_replace-header"] = array('name' => "http-after-response header replace", 'mode'=> 'http', 'syntax' => 'http-after-response replace-header {name} {find} {replace}',
'fields' => array(
array('name'=>"name",'columnheader'=>"Headername",'type'=>"textbox",'size'=>"50",'mask'=>'headername'),
array('name'=>"find",'columnheader'=>"Find regex",'type'=>"textbox",'size'=>"50",'mask'=>'match-regex'),
array('name'=>"replace",'columnheader'=>"Replace by",'type'=>"textbox",'size'=>"50",'mask'=>'replace-fmt')
));
$a_action["http-after-response_replace-value"] = array('name' => "http-after-response header replace value", 'mode'=> 'http', 'syntax' => 'http-after-response replace-value {name} {find} {replace}',
'fields' => array(
array('name'=>"name",'columnheader'=>"Headername",'type'=>"textbox",'size'=>"50",'mask'=>'headername'),
array('name'=>"find",'columnheader'=>"Find regex",'type'=>"textbox",'size'=>"50",'mask'=>'match-regex'),
array('name'=>"replace",'columnheader'=>"Replace by",'type'=>"textbox",'size'=>"50",'mask'=>'replace-fmt')
));
$a_action["http-after-response_set-status"] = array('name' => "http-after-response set status", 'mode'=> 'http', 'syntax' => ' http-after-response set-status {status} {reason}',
'fields' => array(
array('name'=>"status",'columnheader'=>"Status",'type'=>"textbox",'size'=>"50",'mask'=>'text'),
array('name'=>"reason",'columnheader'=>"Reason",'type'=>"textbox",'size'=>"50",'mask'=>'text','prefix'=>'reason')
));
}
//
$a_action["tcp-request_connection_accept"] = array('name' => "tcp-request connection accept", 'mode'=> '', 'syntax' => 'tcp-request connection accept');
$a_action["tcp-request_connection_reject"] = array('name' => "tcp-request connection reject", 'mode'=> '', 'syntax' => 'tcp-request connection reject');
//
$a_action["tcp-request_content_accept"] = array('name' => "tcp-request content accept", 'mode'=> '', 'syntax' => 'tcp-request content accept');
$a_action["tcp-request_content_reject"] = array('name' => "tcp-request content reject", 'mode'=> '', 'syntax' => 'tcp-request content reject');
if (haproxy_version() >= '1.6') {
$a_action["tcp-request_content_lua"] = array('name' => "tcp-request content lua script", 'mode'=> '', 'syntax' => 'tcp-request content lua.{lua-function}',
'fields' => array(
'lua-function' => array('name'=>"lua-function",'columnheader'=>"lua function",'type'=>"textbox",'size'=>"50",'mask'=>'lua-function')
));
$a_action["tcp-request_content_use-service"] = array('name' => "tcp-request content use-service", 'mode'=> '', 'syntax' => 'tcp-request content use-service lua.{lua-function}',
'fields' => array(
'lua-function' => array('name'=>"lua-function",'columnheader'=>"lua function",'type'=>"textbox",'size'=>"50",'mask'=>'lua-function')
));
}
//
$a_action["tcp-response_content_accept"] = array('name' => "tcp-response content accept", 'mode'=> '', 'syntax' => 'tcp-response content accept');
$a_action["tcp-response_content_close"] = array('name' => "tcp-response content close", 'mode'=> '', 'syntax' => 'tcp-response content close');
$a_action["tcp-response_content_reject"] = array('name' => "tcp-response content reject", 'mode'=> '', 'syntax' => 'tcp-response content reject');
if (haproxy_version() >= '1.6') {
$a_action["tcp-response_content_lua"] = array('name' => "tcp-response content lua script", 'mode'=> '', 'syntax' => 'tcp-response content lua.{lua-function}', 'usage' => 'backend',
'fields' => array(
'lua-function' => array('name'=>"lua-function",'columnheader'=>"lua function",'type'=>"textbox",'size'=>"50",'mask'=>'lua-function')
));
}
#end
global $haproxy_version;
function haproxy_version() {
global $haproxy_version;
if (empty($haproxy_version)) {
$haproxy_version = shell_exec("/usr/local/sbin/haproxy -v | head -n 1 | awk '{ print $3 }'");
}
return $haproxy_version;
}
function haproxy_portoralias_to_list($port_or_alias) {
// input: a port or aliasname: 80 https MyPortAlias
// returns: a array of ports and portranges 80 443 8000:8010
global $aliastable;
$portresult = array();
if (alias_get_type($port_or_alias) == "port") {
$aliasports = $aliastable[$port_or_alias];
$ports = explode(' ',$aliasports);
foreach($ports as $port) {
$portresults = haproxy_portoralias_to_list($port);
$portresult = array_merge($portresult, $portresults);
}
return $portresult;
} elseif (is_portrange($port_or_alias)) {
return (array)$port_or_alias;
} else {
$ports = explode(",", $port_or_alias);
foreach($ports as $port){
if (is_port($port)) {
if (getservbyname($port, "tcp")) {
$port = getservbyname($port, "tcp");
}
if (getservbyname($port, "udp")) {
$port = getservbyname($port, "udp");
}
$portresult[] = $port;
}
}
return $portresult;
}
}
function haproxy_expand_alias_array($address_or_alias) {
global $aliastable;
$result = array();
$items = explode(' ', $address_or_alias);
$i = 0;
while($i < count($items)) {
$item = $items[$i];
$alias_type = alias_get_type($item);
$alias = $aliastable[$item];
if ($alias_type == "url") {
$items = array_merge($items, explode(' ',$alias));
} elseif ($alias_type == "urltable") {
$urltable = alias_expand_urltable($item);
if (!empty($urltable)) {
$urltable_arr = array();
$urlfile_as_arr = file($urltable);
foreach ($urlfile_as_arr as $line) {
$urltable_arr[] = rtrim($line);
}
$items = array_merge($items, $urltable_arr);
}
} elseif ($alias_type == "network") {
$items = array_merge($items, explode(' ',$alias));
} elseif ($alias_type == "host") {
$items = array_merge($items, explode(' ',$alias));
} else {
$result[] = $item;
}
$i++;
}
return $result;
}
function haproxy_filter_alias_array($list, $ip=false, $subnet=false, $hostname=false, $convertiptosubnet=false) {
$result = array();
foreach($list as $item) {
if ($ip && $ipv = is_ipaddr($item)) {
if ($convertiptosubnet) {
if ($ipv == 4) {
$item .= "/32";
} elseif ($ipv == 6) {
$item .= "/128";
}
}
$result[] = $item;
} elseif ($subnet && is_subnet($item)) {
$result[] = $item;
} elseif ($hostname && is_hostname($item)) {
$result[] = $item;
}
}
return $result;
}
function haproxy_hostoralias_to_list($host_or_alias) {
//used by source_ip acl
$items = haproxy_expand_alias_array($host_or_alias);
$result = haproxy_filter_alias_array($items, true, true);
return $result;
}
function haproxy_get_fileslist() {
// returns the files array with 'keys'.
$result = array();
// create a copy to not modify the original 'keyless' array
$a_files = config_get_path('installedpackages/haproxy/files/item');
foreach($a_files as $file) {
$key = $file['name'];
$result[$key] = $file;
}
return $result;
}
function haproxy_custom_php_deinstall_command() {
global $static_output;
$static_output .= "HAProxy, running haproxy_custom_php_deinstall_command()\n";
update_output_window($static_output);
$static_output .= "HAProxy, deleting haproxy webgui\n";
update_output_window($static_output);
unlink_if_exists("/usr/local/etc/rc.d/haproxy.sh");
unlink_if_exists("/etc/rc.haproxy_ocsp.sh");
$static_output .= "HAProxy, uninstalling cron job if needed\n";
update_output_window($static_output);
install_cron_job("/usr/local/etc/rc.d/haproxy.sh onecheck", false);
install_cron_job("/etc/rc.haproxy_ocsp.sh", false);
$static_output .= "HAProxy, running haproxy_custom_php_deinstall_command() DONE\n";
update_output_window($static_output);
}
function haproxy_custom_php_install_command() {
global $g, $static_output;
$static_output .= "HAProxy, running haproxy_custom_php_install_command()\n";
update_output_window($static_output);
$haproxy_binary = "/usr/local/sbin/haproxy";
$static_output .= "HAProxy, create '/usr/local/etc/rc.d/haproxy.sh'\n";
update_output_window($static_output);
$haproxy = <<<EOD
#!/bin/sh
# PROVIDE: haproxy
# REQUIRE: LOGIN
# KEYWORD: FreeBSD
. /etc/rc.subr
# rc_fast=yes workaround for pfSense that calls start when it
# means restart (for a wan-ip change for example..)
# this way it doesnt check for a already running process
# and just fires of the start procedure again. which will
# take care to restart haproxy gracefully
rc_fast=yes
name="haproxy"
rcvar="\${name}_enable"
command="{$haproxy_binary}"
haproxy_enable=\${haproxy-"YES"}
start_cmd="haproxy_start"
stop_postcmd="haproxy_stop"
restart_cmd="haproxy_restart"
check_cmd="haproxy_check"
extra_commands="check"
load_rc_config \$name
haproxy_start () {
echo "Starting haproxy."
/usr/bin/env \
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
/usr/local/bin/php-cgi -q -d auto_prepend_file=config.inc <<ENDOFF
<?php
require_once("globals.inc");
require_once("functions.inc");
require_once("haproxy/haproxy.inc");
haproxy_configure();
?>
ENDOFF
}
haproxy_check () {
echo "Checking haproxy."
/usr/bin/env \
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
/usr/local/bin/php-cgi -q -d auto_prepend_file=config.inc <<ENDOFF
<?php
require_once("globals.inc");
require_once("functions.inc");
require_once("haproxy/haproxy.inc");
haproxy_check_run(0);
?>
ENDOFF
}
haproxy_stop () {
echo "Stopping haproxy."
killall haproxy
}
haproxy_restart () {
echo "Restarting haproxy."
haproxy_start
}
run_rc_command "\$1"
EOD;
$fd = fopen("/usr/local/etc/rc.d/haproxy.sh", "w");
fwrite($fd, $haproxy);
fclose($fd);
chmod("/usr/local/etc/rc.d/haproxy.sh", 0755);
$haproxy_ocsp = <<<EOD
#!/usr/local/bin/php-cgi -f
<?php
/*
Updates haproxy OCSP responses.
*/
require_once("globals.inc");
require_once("functions.inc");
require_once("haproxy/haproxy.inc");
require_once("haproxy/haproxy_socketinfo.inc");
haproxy_updateocsp();
?>
EOD;
// removing the \r prevents the "No input file specified." error..
$haproxy_ocsp = str_replace("\r\n","\n", $haproxy_ocsp);
$fd = fopen("/etc/rc.haproxy_ocsp.sh", "w");
fwrite($fd, $haproxy_ocsp);
fclose($fd);
chmod("/etc/rc.haproxy_ocsp.sh", 0755);
$static_output .= "HAProxy, update configuration\n";
update_output_window($static_output);
// call from external file, so it is surely from the newly downloaded version
// this avoids a problem with php cache that loaded haproxy.inc during uninstalling the old version
require_once('haproxy_upgrade_config.inc');
haproxy_upgrade_config();
$static_output .= "HAProxy, starting haproxy (if previously enabled)\n";
update_output_window($static_output);
haproxy_check_run(1);
$static_output .= "HAProxy, running haproxy_custom_php_install_command() DONE\n";
update_output_window($static_output);
}
function haproxy_find_backend($backendname) {
foreach (config_get_path('installedpackages/haproxy/ha_pools/item', []) as $backend) {
if ($backend['name'] == $backendname) {
return $backend;
}
}
return null;
}
function haproxy_find_acl($name) {
global $a_acltypes;
if ($a_acltypes) {
foreach ($a_acltypes as $key => $acl) {
if ($key == $name) {
return $acl;
}
}
}
}
function write_backend($configpath, $fd, $name, $pool, $backendsettings) {
$frontend = $backendsettings['frontend'];
$ipversion = $backendsettings['ipversion'];
$a_global = config_get_path('installedpackages/haproxy', []);
$a_mailers = config_get_path('installedpackages/haproxy/email_mailers/item');
$a_resolvers = config_get_path('installedpackages/haproxy/dns_resolvers/item');
$a_servers = getarraybyref($pool,'ha_servers','item');
global $a_checktypes, $a_cookiemode, $a_files_cache, $a_error;
$server_options = "";
$frontendtype = $frontend['type'];
$idoffset = 0;
if ($ipversion == "ipv4") {
$idoffset += 10000;
}
if ($ipversion == "ipv6") {
$idoffset += 20000;
}
fwrite ($fd, "backend " . $name . "\n");
// https is an alias for tcp for clarity purposes
if ($frontendtype == "https") {
$backend_mode = "tcp";
} else {
$backend_mode = $frontendtype;
}
fwrite ($fd, "\tmode\t\t\t" . $backend_mode . "\n");
$id = "";
if (is_numeric($pool['id'])) {
$id = ($pool['id'] + $idoffset);
fwrite ($fd, "\tid\t\t\t{$id}\n");
}
fwrite ($fd, "\tlog\t\t\tglobal\n");
$use_haproxyresolvers = false;
if (haproxy_version() >= '1.6') {
$use_mailers = is_array($a_mailers) && count($a_mailers) > 0;
if ($use_mailers) {
fwrite ($fd, "\t# use mailers\n");
if (empty($pool['email_level'])) {
$email_level = $a_global['email_level'];
} else {
$email_level = $pool['email_level'];
}
fwrite ($fd, "\t# level $email_level \n");
if (!empty($email_level) && $email_level != 'dontlog') {
if (empty($pool['email_to'])) {
$email_to = $a_global['email_to'];
} else {
$email_to = $pool['email_to'];
}
fwrite ($fd, "\temail-alert mailers\t\t\tglobalmailers\n");
fwrite ($fd, "\temail-alert level\t\t\t{$email_level}\n");
fwrite ($fd, "\temail-alert from\t\t\t{$a_global['email_from']}\n");
fwrite ($fd, "\temail-alert to\t\t\t{$email_to}\n");
if (!empty($a_global['email_myhostname'])) {
fwrite ($fd, "\temail-alert myhostname\t\t\t{$a_global['email_myhostname']}\n");
}
}
}
$use_resolvers = is_array($a_resolvers) && count($a_resolvers) > 0;
if ($use_resolvers) {
$use_haproxyresolvers = true;
//server s1 app1.domain.com:80 resolvers mydns resolve-prefer ipv6
$resolverprefer = ($ipversion == "ipv4" || $ipversion == "ipv6") ? $resolverprefer = " resolve-prefer {$ipversion}" : "";
$server_options .= " resolvers globalresolvers" . $resolverprefer;
}
}
if ($pool['log-health-checks'] == 'yes') {
fwrite ($fd, "\toption\t\t\tlog-health-checks\n");
}
if ($frontendtype == "http") {
// actions that read/write http headers only work when 'mode http' is used
if ($pool["persist_cookie_enabled"] == "yes") {
$cookie_mode = $pool["persist_cookie_mode"];
$cookie_cachable = $pool["persist_cookie_cachable"];
$cookiesyntax = $a_cookiemode[$cookie_mode]["syntax"];
$cookie = str_replace("<cookie name>", $pool["persist_cookie_name"], $cookiesyntax);
$cookie .= $cookie_cachable == "yes" ? "" : " nocache";
$cookie_parameters = "";
if ($pool["persist_cookie_postonly"] == "yes") {
$cookie_parameters .= " postonly";
}
if ($pool["persist_cookie_httponly"] == "yes") {
$cookie_parameters .= " httponly";
}
if ($pool["persist_cookie_secure"] == "yes") {
$cookie_parameters .= " secure";
}
if (!empty($pool["haproxy_cookie_maxidle"])) {
$cookie_parameters .= " maxidle ".$pool["haproxy_cookie_maxidle"];
}
if (!empty($pool["haproxy_cookie_maxlife"])) {
$cookie_parameters .= " maxlife ".$pool["haproxy_cookie_maxlife"];
}
if (!empty($pool["haproxy_cookie_dynamic_cookie_key"])) {
$cookie_parameters .= " dynamic";
}
$cookiedomains = explode(" ",$pool["haproxy_cookie_domains"]);
foreach($cookiedomains as $cookiedomain) {
$cookiedomain = trim($cookiedomain);
if (!empty($cookiedomain)) {
$cookie_parameters .= " domain " . $cookiedomain;
}
}
fwrite ($fd, "\t" . $cookie . $cookie_parameters . "\n");
if (!empty($pool["haproxy_cookie_dynamic_cookie_key"])) {
fwrite ($fd, "\tdynamic-cookie-key " . $pool["haproxy_cookie_dynamic_cookie_key"] . "\n");
}
}
if ($pool["strict_transport_security"] && is_numeric($pool["strict_transport_security"])){
fwrite ($fd, "\thttp-response set-header Strict-Transport-Security max-age={$pool["strict_transport_security"]};\n");
}
if ($pool["cookie_attribute_secure"] == 'yes'){
fwrite ($fd, "\thttp-response replace-header Set-Cookie \"^((?:(?!; [Ss]ecure\b).)*)\\$\" \"\\1; secure\" if { ssl_fc }\n");
}
if ($pool['stats_enabled'] == 'yes') {
fwrite ($fd, "\tstats\t\t\tenable\n");
if ($pool['stats_uri']) {
fwrite ($fd, "\tstats\t\t\turi ".$pool['stats_uri']."\n");
}
if ($pool['stats_realm']) {
fwrite ($fd, "\tstats\t\t\trealm " . haproxy_escapestring($pool['stats_realm']) . "\n");
} else {
fwrite ($fd, "\tstats\t\t\trealm .\n");
}
if ($pool['stats_username'] && $pool['stats_password']) {
fwrite ($fd, "\tstats\t\t\tauth " . haproxy_escapestring($pool['stats_username']).":". haproxy_escapestring($pool['stats_password'])."\n");
}
if ($pool['stats_admin'] == 'yes') {
fwrite ($fd, "\tstats\t\t\tadmin if TRUE" . "\n");
}
if ($pool['stats_node']) {
fwrite ($fd, "\tstats\t\t\tshow-node " . $pool['stats_node'] . "\n");
}
if ($pool['stats_desc']) {
fwrite ($fd, "\tstats\t\t\tshow-desc " . haproxy_escapestring($pool['stats_desc']) . "\n");
}
if ($pool['stats_refresh']) {
fwrite ($fd, "\tstats\t\t\trefresh " . $pool['stats_refresh'] . "\n");
}
if ($pool['stats_scope']) {
$scope_items = explode(",", $pool['stats_scope']);
foreach($scope_items as $scope_item) {
fwrite ($fd, "\tstats\t\t\tscope " . $scope_item . "\n");
}
}
}
$errorfiles = getarraybyref($pool,'errorfiles','item');
foreach($errorfiles as $errorfile) {
if (!is_array($a_files_cache)) {// load only once
$a_files_cache = haproxy_get_fileslist();
}
$file = $errorfile['errorfile'];
$errorcodes = explode(",",$errorfile['errorcode']);
foreach($errorcodes as $errorcode) {
$filename = "$configpath/errorfile_{$name}_{$errorcode}_{$file}";
$content = base64_decode($a_files_cache[$file]['content']);
$content = str_replace('{errormsg}', $a_error[$errorcode]['descr'], $content);
$content = str_replace('{errorcode}', $errorcode, $content);
file_put_contents($filename, $content);
fwrite ($fd, "\terrorfile\t\t\t" . $errorcode ." " . $filename . "\n");
}
}
}
switch($pool["persist_sticky_type"]) {
case 'stick_sslsessionid':
if ($frontendtype == "https") {
fwrite ($fd, "\ttcp-request inspect-delay 5s\n");
fwrite ($fd, "\tstick-table type binary len 32 size ".$pool["persist_stick_tablesize"]." expire ".$pool["persist_stick_expire"]."\n");
fwrite ($fd, "\tacl clienthello req.ssl_hello_type 1\n");
fwrite ($fd, "\tacl serverhello res.ssl_hello_type 2\n");
fwrite ($fd, "\ttcp-request content accept if clienthello\n");
fwrite ($fd, "\ttcp-response content accept if serverhello\n");
fwrite ($fd, "\tstick on payload_lv(43,1) if clienthello\n");
fwrite ($fd, "\tstick store-response payload_lv(43,1) if serverhello\n");
}
break;
case 'stick_rdp_cookie':
//tcp-request content accept if RDP_COOKIE
//fwrite ($fd, "\tstick on req.rdp_cookie(msts)\n");
fwrite ($fd, "\tstick-table type binary len 32 size ".$pool["persist_stick_tablesize"]." expire ".$pool["persist_stick_expire"]."\n");
fwrite ($fd, "\tstick on req.rdp_cookie(mstshash)\n");
break;
case 'stick_sourceipv4':
fwrite ($fd, "\tstick-table type ip size ".$pool["persist_stick_tablesize"]." expire ".$pool["persist_stick_expire"]."\n");
fwrite ($fd, "\tstick on src\n");
break;
case 'stick_sourceipv6':
fwrite ($fd, "\tstick-table type ip size ".$pool["persist_stick_tablesize"]." expire ".$pool["persist_stick_expire"]."\n");
fwrite ($fd, "\tstick on src\n");
break;
case 'stick_cookie_value':
if ($frontendtype == "http") {
fwrite ($fd, "\tstick-table type string len {$pool["persist_stick_length"]} size ".$pool["persist_stick_tablesize"]." expire ".$pool["persist_stick_expire"]."\n");
fwrite ($fd, "\tstick store-response res.cook({$pool["persist_stick_cookiename"]})\n");
fwrite ($fd, "\tstick on req.cook({$pool["persist_stick_cookiename"]})\n");
}
break;
}
$check_type = $pool['check_type'];
if ($check_type != 'none') {
$optioncheck = $a_checktypes[$check_type]['syntax'];
if ($check_type == "MySQL" || $check_type == "PostgreSQL") {
$optioncheck .= " user " . $pool['monitor_username'];
}
if ($check_type == "SMTP" || $check_type == "ESMTP") {
$optioncheck .= " " . $pool['monitor_domain'];
}
if ($check_type == "HTTP") {
$uri = $pool['monitor_uri'];
if (!$uri) {
$uri = "/";
}
$optioncheck .= " {$pool['httpcheck_method']} {$uri} {$pool['monitor_httpversion']}";
}
if ($check_type == "Agent") {
$checkport = " port " . $pool['monitor_agentport'];
}
if (($check_type == "HTTP") && (haproxy_version() >= '2.2')) {
$httpchecksend = "\thttp-check\t\tsend meth {$pool['httpcheck_method']}";
if (!empty($pool['monitor_uri'])) {
$httpchecksend .= " uri {$pool['monitor_uri']}";
}
if (!empty($pool['monitor_httpversion'])) {
$httpchecksend .= " ver {$pool['monitor_httpversion']}";
}
fwrite ($fd, $httpchecksend . "\n");
$optioncheck = "httpchk";
}
}
if ($pool['balance']) {
$parameters = "";
if ($pool['balance'] == 'uri') {
if (!empty($pool['balance_urilen'])) {
$parameters .= " len {$pool['balance_urilen']}";
}
if (!empty($pool['balance_uridepth'])) {
$parameters .= " depth {$pool['balance_uridepth']}";
}
if ($pool['balance_uriwhole'] == 'yes') {
$parameters .= " whole";
}
}
fwrite ($fd, "\tbalance\t\t\t{$pool['balance']}{$parameters}\n");
}
if (empty($pool['connection_timeout'])) {
$pool['connection_timeout'] = 30000;
}
fwrite ($fd, "\ttimeout connect\t\t" . $pool['connection_timeout'] . "\n");
if (empty($pool['server_timeout'])) {
$pool['server_timeout'] = 30000;
}
fwrite ($fd, "\ttimeout server\t\t" . $pool['server_timeout'] . "\n");