(DockerClient client, String localIp, String proto)
| 186 | } |
| 187 | |
| 188 | public void patchContainer(DockerClient client, String localIp, String proto) { |
| 189 | try { |
| 190 | |
| 191 | List<OpenVPNForwardPort> forwardPorts = OpenVPNForwardPorts.getInstance().queryAll(); |
| 192 | for (OpenVPNForwardPort forwardPort : forwardPorts) { |
| 193 | |
| 194 | String command = "/sbin/iptables -t nat -A PREROUTING -p " + forwardPort.getType().toString() |
| 195 | + " --dport " + forwardPort.getFromPort() + " -j DNAT --to-destination " + localIp + ":" |
| 196 | + forwardPort.getToPort(); |
| 197 | |
| 198 | String[] commands = new String[]{"/bin/sh", "-c", command}; |
| 199 | execCommand(client, commands); |
| 200 | } |
| 201 | |
| 202 | // patch server/client configs |
| 203 | // change the server config to use inside the volume |
| 204 | String[] commands = {"/bin/sh", "-c", |
| 205 | "\"sed -i 's/\\/etc\\/openvpn\\/server\\.conf/\\/opt\\/Dockovpn\\/config\\/server\\.conf/' /opt/Dockovpn/start.sh\""}; |
| 206 | execCommand(client, commands); |
| 207 | // change server/client config file(udp->tcp-server/tcp-client) |
| 208 | switch (proto) { |
| 209 | |
| 210 | case "TCP" : |
| 211 | commands = new String[]{"/bin/sh", "-c", |
| 212 | "sed -i s/udp/tcp-server/ /opt/Dockovpn/config/server.conf"}; |
| 213 | execCommand(client, commands); |
| 214 | commands = new String[]{"/bin/sh", "-c", |
| 215 | "find /opt -name \"client.ovpn\" | xargs sed -i s/udp/tcp-client/"}; |
| 216 | execCommand(client, commands); |
| 217 | break; |
| 218 | case "UDP" : |
| 219 | commands = new String[]{"/bin/sh", "-c", |
| 220 | "sed -i s/tcp-server/udp/ /opt/Dockovpn/config/server.conf"}; |
| 221 | execCommand(client, commands); |
| 222 | commands = new String[]{"/bin/sh", "-c", |
| 223 | "find /opt -name \"client.ovpn\" | xargs sed -i s/tcp-client/udp/"}; |
| 224 | execCommand(client, commands); |
| 225 | break; |
| 226 | } |
| 227 | /* |
| 228 | * restart the server. it is because |
| 229 | * - dockovpn is start in entrypoint, this means it is difficult to patch in CMD |
| 230 | * - so it seems to be necessary to patch in docker exec, but it is after docker |
| 231 | * start(now doing) |
| 232 | * - after patching, the server should be restarted to reflect the config |
| 233 | */ |
| 234 | log("OpenVPN Server is restarting..."); |
| 235 | // kill the process and restart openvpn |
| 236 | commands = new String[]{"/bin/sh", "-c", "kill $(pgrep openvpn)"}; |
| 237 | execCommand(client, commands); |
| 238 | commands = new String[]{"openvpn", "--config", "/opt/Dockovpn/config/server.conf"}; |
| 239 | execCommand(client, commands); |
| 240 | } catch (Exception e) { |
| 241 | |
| 242 | errWithStackTrace(e); |
| 243 | } |
| 244 | } |
| 245 |
no test coverage detected