前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自己动手写个中转服务器

自己动手写个中转服务器

原创
作者头像
谭广健
发布2024-05-19 14:26:02
1340
发布2024-05-19 14:26:02
举报
文章被收录于专栏:谭广健的专栏谭广健的专栏

某个涉密项目内部与互联网完全隔绝,只靠一台边界机接入互联网;并且该边界机只能通过特地端口进行内网数据传输。现在的需求是边界机通过互联网获取信息,然后再在特定端口进行复制。以前一般做个数据采集到本地,然后转化就完事。但这次数据一落地就被处理。哪怎么办?本来想通过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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档