public async Task <ResponseViewModel <bool> > AddTunnel(int userId, AdminTunnelsViewModel vm) { var data = new ResponseViewModel <bool>(); if (await _tunnelProvider.GetTunnelBySubDomail(vm.SubDomain) != null) { data.Code = 1; data.Msg = _localizer["add_tunnel.msg"]; return(data); } var tunnelId = GuidUtil.GenerateGuid(); if (await _tunnelProvider.GetTunnel(tunnelId) != null) { data.Code = 1; data.Msg = _localizer["add_tunnel.error"]; return(data); } var tunnel = new TunnelsEntity { TunnelId = tunnelId, UserId = userId, TunnelProtocol = vm.TunnelProtocol, TunnelName = vm.TunnelName, SubDomain = vm.SubDomain, LocalPort = vm.LocalPort, Status = (int)TunnelStatusEnum.Pass, CreateTime = DateTime.Now, OpenTime = DateTime.Now, ExpiredTime = vm.ExpiredTime, FullUrl = vm.FullUrl }; var user = await _usersProvider.GetUserInfo(userId); try { await FileUtil.WriteTunnel(tunnel, user, _tunnelConfig.FilePath, vm.RemotePort); } catch (Exception e) { data.Code = 1; data.Msg = e.Message; return(data); } data.Data = await _tunnelProvider.Create(tunnel); return(data); }
public async Task <ResponseViewModel <bool> > AddTunnel(TunnelsViewModel vm) { var data = new ResponseViewModel <bool>(); var userId = HttpContext.User.FindFirst(u => u.Type == ClaimTypes.NameIdentifier).Value; if (await _tunnelProvider.GetTunnelBySubDomail(vm.SubDomain) != null) { data.Code = 1; data.Msg = _localizer["add_tunnel.msg"]; return(data); } var tunnelId = GuidUtil.GenerateGuid(); if (await _tunnelProvider.GetTunnel(tunnelId) != null) { data.Code = 1; data.Msg = _localizer["add_tunnel.error"]; return(data); } var tunnel = new TunnelsEntity { TunnelId = tunnelId, UserId = Convert.ToInt32(userId), TunnelProtocol = vm.TunnelProtocol, TunnelName = vm.TunnelName, SubDomain = vm.SubDomain, LocalPort = vm.LocalPort, FullUrl = vm.FullUrl, Status = (int)TunnelStatusEnum.Audit, CreateTime = DateTime.Now }; data.Data = await _tunnelProvider.Create(tunnel); return(data); }