package com.educoder.bridge.controller; import com.alibaba.fastjson.JSONObject; import com.educoder.bridge.model.Webssh; import com.educoder.bridge.service.WebsshService; import com.educoder.bridge.utils.Base64Util; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; /** * @author guange * @date 2017/08/02 */ @Api(value = "提供webssh连接", hidden = true) @RestController public class WebsshController extends BaseController { private final static Logger logger = LoggerFactory.getLogger(WebsshController.class); @Autowired private WebsshService websshService; @ApiOperation(value = "获取连接容器所需的ip和端口信息", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE) @RequestMapping(path = "/webssh/getConnectInfo") public JSONObject getConnectInfo( @ApiParam(name = "tpiID", required = true, value = "tpiID") @RequestParam String tpiID, @ApiParam(name = "podType", required = true, value = "pod类型,0为evaluate, 1为webssh,2为evassh") @RequestParam Integer podType, @ApiParam(name = "containers", required = true, value = "需要使用的容器,base64编码") @RequestParam String containers) throws Exception { logger.info("tpiID: {}, podType: {},containers: {}", tpiID, podType, containers); containers = Base64Util.decode(containers); Webssh sshInfo = websshService.create(tpiID, podType, containers); JSONObject response = new JSONObject(); response.put("address", sshInfo.getAddress()); response.put("port", Integer.parseInt(sshInfo.getPort()) + ""); response.put("code", 0); return response; } @RequestMapping(value={"/", "ssh"}, method= RequestMethod.GET) public ModelAndView index(@RequestParam("Host")String host, @RequestParam("Port")int port, @RequestParam("Gameid")int gameId, @RequestParam("Username")String username, @RequestParam("Password")String password, @RequestParam("Tab")String tab, @RequestParam("Rows")int rows) { //index就是视图的名称(index.ftl) logger.debug("/ssh: 接收到前端连接请求,host: {}, port: {}", host, port); ModelAndView mv = new ModelAndView(); mv.setViewName("index"); mv.addObject("Host", host); mv.addObject("Port", port); mv.addObject("Username", username); mv.addObject("Password", password); mv.addObject("Tab", tab); mv.addObject("Rows", rows); mv.addObject("Gameid", gameId); mv.addObject("digest", System.currentTimeMillis()); return mv; } }