diff --git a/src/main/java/com/litoralregas/openvpn/vps/VpsController.java b/src/main/java/com/litoralregas/openvpn/vps/VpsController.java new file mode 100644 index 0000000..6f3bd61 --- /dev/null +++ b/src/main/java/com/litoralregas/openvpn/vps/VpsController.java @@ -0,0 +1,37 @@ +package com.litoralregas.openvpn.vps; + +import com.litoralregas.openvpn.ssh.SshCommandResult; +import com.litoralregas.openvpn.ssh.SshService; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api/vps") +public class VpsController { + + private final SshService sshService; + + public VpsController(SshService sshService) { + this.sshService = sshService; + } + + @PostMapping("/openvpn/restart") + public ResponseEntity restartOpenVpn() { + + SshCommandResult restart = sshService.executeOnConfiguredVps( + "sudo systemctl restart openvpn-server@server" + ); + + if (restart.exitCode() != 0) { + return ResponseEntity.internalServerError().body(restart); + } + + SshCommandResult status = sshService.executeOnConfiguredVps( + "systemctl is-active openvpn-server@server" + ); + + return ResponseEntity.ok(status); + } +} \ No newline at end of file