Project

General

Profile

« Previous | Next » 

Revision 61fce4a6

Added by Phil Davis over 9 years ago

Consolidate OpenVPN kill_client routine

After a gitsync just now I started getting "cannot redeclare
kill_client" error messages. Whatever it was that caused this to start
happening, the kill_client() function in these 2 places is identical.
Might as well put it in openvpn.inc

View differences:

src/etc/inc/openvpn.inc
1611 1611
	return $client;
1612 1612
}
1613 1613

  
1614
function openvpn_kill_client($port, $remipp) {
1615
	global $g;
1616

  
1617
	//$tcpsrv = "tcp://127.0.0.1:{$port}";
1618
	$tcpsrv = "unix://{$g['varetc_path']}/openvpn/{$port}.sock";
1619
	$errval = null;
1620
	$errstr = null;
1621

  
1622
	/* open a tcp connection to the management port of each server */
1623
	$fp = @stream_socket_client($tcpsrv, $errval, $errstr, 1);
1624
	$killed = -1;
1625
	if ($fp) {
1626
		stream_set_timeout($fp, 1);
1627
		fputs($fp, "kill {$remipp}\n");
1628
		while (!feof($fp)) {
1629
			$line = fgets($fp, 1024);
1630

  
1631
			$info = stream_get_meta_data($fp);
1632
			if ($info['timed_out']) {
1633
				break;
1634
			}
1635

  
1636
			/* parse header list line */
1637
			if (strpos($line, "INFO:") !== false) {
1638
				continue;
1639
			}
1640
			if (strpos($line, "SUCCESS") !== false) {
1641
				$killed = 0;
1642
			}
1643
			break;
1644
		}
1645
		fclose($fp);
1646
	}
1647
	return $killed;
1648
}
1649

  
1614 1650
function openvpn_refresh_crls() {
1615 1651
	global $g, $config;
1616 1652

  
src/usr/local/www/status_openvpn.php
75 75
		$port  = $_GET['port'];
76 76
		$remipp  = $_GET['remipp'];
77 77
		if (!empty($port) and !empty($remipp)) {
78
			$retval = kill_client($port, $remipp);
78
			$retval = openvpn_kill_client($port, $remipp);
79 79
			echo htmlentities("|{$port}|{$remipp}|{$retval}|");
80 80
		} else {
81 81
			echo gettext("invalid input");
......
84 84
	}
85 85
}
86 86

  
87

  
88
function kill_client($port, $remipp) {
89
	global $g;
90

  
91
	//$tcpsrv = "tcp://127.0.0.1:{$port}";
92
	$tcpsrv = "unix://{$g['varetc_path']}/openvpn/{$port}.sock";
93
	$errval = null;
94
	$errstr = null;
95

  
96
	/* open a tcp connection to the management port of each server */
97
	$fp = @stream_socket_client($tcpsrv, $errval, $errstr, 1);
98
	$killed = -1;
99
	if ($fp) {
100
		stream_set_timeout($fp, 1);
101
		fputs($fp, "kill {$remipp}\n");
102
		while (!feof($fp)) {
103
			$line = fgets($fp, 1024);
104

  
105
			$info = stream_get_meta_data($fp);
106
			if ($info['timed_out']) {
107
				break;
108
			}
109

  
110
			/* parse header list line */
111
			if (strpos($line, "INFO:") !== false) {
112
				continue;
113
			}
114
			if (strpos($line, "SUCCESS") !== false) {
115
				$killed = 0;
116
			}
117
			break;
118
		}
119
		fclose($fp);
120
	}
121
	return $killed;
122
}
123

  
124 87
$servers = openvpn_get_active_servers();
125 88
$sk_servers = openvpn_get_active_servers("p2p");
126 89
$clients = openvpn_get_active_clients();
src/usr/local/www/widgets/widgets/openvpn.widget.php
65 65
		$port = $_GET['port'];
66 66
		$remipp = $_GET['remipp'];
67 67
		if (!empty($port) and !empty($remipp)) {
68
			$retval = kill_client($port, $remipp);
68
			$retval = openvpn_kill_client($port, $remipp);
69 69
			echo htmlentities("|{$port}|{$remipp}|{$retval}|");
70 70
		} else {
71 71
			echo gettext("invalid input");
......
74 74
	}
75 75
}
76 76

  
77

  
78
function kill_client($port, $remipp) {
79
	global $g;
80

  
81
	//$tcpsrv = "tcp://127.0.0.1:{$port}";
82
	$tcpsrv = "unix://{$g['varetc_path']}/openvpn/{$port}.sock";
83
	$errval = null;
84
	$errstr = null;
85

  
86
	/* open a tcp connection to the management port of each server */
87
	$fp = @stream_socket_client($tcpsrv, $errval, $errstr, 1);
88
	$killed = -1;
89
	if ($fp) {
90
		stream_set_timeout($fp, 1);
91
		fputs($fp, "kill {$remipp}\n");
92
		while (!feof($fp)) {
93
			$line = fgets($fp, 1024);
94

  
95
			$info = stream_get_meta_data($fp);
96
			if ($info['timed_out']) {
97
				break;
98
			}
99

  
100
			/* parse header list line */
101
			if (strpos($line, "INFO:") !== false) {
102
				continue;
103
			}
104
			if (strpos($line, "SUCCESS") !== false) {
105
				$killed = 0;
106
			}
107
			break;
108
		}
109
		fclose($fp);
110
	}
111
	return $killed;
112
}
113

  
114 77
$servers = openvpn_get_active_servers();
115 78
$sk_servers = openvpn_get_active_servers("p2p");
116 79
$clients = openvpn_get_active_clients();

Also available in: Unified diff