自己动手写个中转服务器

2024-05-19 14:26:02 浏览数 (1)

某个涉密项目内部与互联网完全隔绝,只靠一台边界机接入互联网;并且该边界机只能通过特地端口进行内网数据传输。现在的需求是边界机通过互联网获取信息,然后再在特定端口进行复制。以前一般做个数据采集到本地,然后转化就完事。但这次数据一落地就被处理。哪怎么办?本来想通过owin技术的,但看了一下还是有点复杂。。突然间想起以前认识的一个兄弟(大石头

)它家有个基于NET6.0的Web服务器于是就研究了一下,感觉提容易上手。那就去马。。

首先新建一个“控制台应用”,然后nuget他们家的两个数据包(NewLife.Core、NewLife.Remoting)。再在Program.cs 写入

代码语言:txt复制
using NewLife.Http;
using NewLife.Log;
using NewLife.Remoting;
using ServerAPP;

XTrace.UseConsole();

var server = new HttpServer
{
    Port = 8080,
    Log = XTrace.Log,
    SessionLog = XTrace.Log
};
server.Map("/", () => "<h1>This is a test~</h1></br> "   DateTime.Now.ToFullString()   "</br><img src="logos/leaf.png" />");
server.Map("/user", (String act, Int32 uid) => new { code = 0, data = $"User.{act}({uid}) success!" });
server.Map("/ws", new MyWebSocket());
server.MapStaticFiles("/logos", "images/");
//server.MapController<ApiController>("/api");
server.MapController<FileServer>("/My");
server.Start();

Console.ReadLine();

这么就完成一个Web服务器的应用。其中server.MapController<FileServer>("/My");就是调用相关Controller的方法,Controller

也给个源码吧。。

代码语言:txt复制
using NewLife.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ServerAPP
{  
    public class FileServer 
    {

        //IHttpActionResult
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Code"></param>
        /// <param name="Name"></param>
     
        public string GetRobotStatus(string Code, string Name)
        {
            try
            {
                var robotModel = new { RobotCode = "RobotCode", RobotName = "RobotName", RobotDesc = "RobotDesc", RobotRemark = "RobotRemark" };
                return "Successful!";
            }
            catch (Exception ex)
            {
                return "Exception!";
            }
        }

        /// <summary>
        /// 
        /// </summary>
 
        public string GetRobotStatusJK()
        {
            try
            {
                var robotModel = new { RobotCode = "RobotCode", RobotName = "RobotName", RobotDesc = "RobotDesc", RobotRemark = "RobotRemark" };
                return "Successful!";
            }
            catch (Exception ex)
            {
                return "Exception!";
            }

        }

        public void ProcessRequest(IHttpContext context)
        {
            var name = context.Parameters["name"];
            var html = $"<h2>你好,<span color="red">{name}</span></h2>";
            context.Response.SetResult(html);
        }

    }
}

至于细节就慢慢研究,最后套用他们的flag:学无先后达者为师!。。。over

c#

0 人点赞