const{createApp}=Vue const{createVuetify,useGoTo,useDisplay}=Vuetify var data={alert:{show:false,color:'success',text:'',timeout:0,},theme:{dark:false,},nav:{showDrawer:false,showTOC:true,tocPanel:0,tab:'account',post:{discussionId:444,currentPage:1,targetPage:1,total:8,anchor:0,goToOptions:{container:null,duration:0,easing:'easeInOutCubic',offset:-100,},worker:null,task:[],active:[],apiLock:[],originLike:new Map([]),},related:{block:1,}},search:{width:80,text:null,loading:false,},tags:[{id:26,url:'/t/317a6a296a045c7566775e0c475c',name:'C|CPP',color:'#FFA726',icon:'mdi-language-cpp',},],posts:[{id:5844,num:0,uid:140,content:'\u003Cp\u003E源码已经发布在 Github:https://github.com/cnbatch/cpp20-socks5demo\u003C/p\u003E\u003Cp\u003ERFC 1928 要求的功能几乎都实现了。\u003C/p\u003E支持的功能:IPv4IPv6‘No Auth’ 认证模式用户名 / 密码 认证模式TCP \u003Ccode\u003EConnect\u003C/code\u003ETCP \u003Ccode\u003EBIND\u003C/code\u003EUDP \u003Ccode\u003EAssociate\u003C/code\u003E未实现的功能:GSSAPI 认证模式\u003Cp\u003ESocks5 标准 (RFC 1928) 写着必须实现这个功能,然而我做的只是个 Demo 程序,平时也用不到 GSSAPI ,为了简单起见就不实现了。\u003C/p\u003E已在这些系统测试过:Windows 11 + VS2022FreeBSD 13.4FreeBSD 14.2Debian 12用协程写代码的体验\u003Cp\u003E用了协程之后,思路清晰多了,不像 callback 时那么头大,相对而言轻松多了。\u003C/p\u003EGithub 就有一大堆 Socks5 Server 程序,为什么又造了个新的?\u003Cp\u003E两大原因:\u003C/p\u003E\u003Cp\u003E我自己是 Windows + FreeBSD 用户,正好需要这样的 socks5 server 程序——可以同时在 Windows 与 FreeBSD 运行,能够支持 TCP + UDP, IPv4 + IPv6 ,使用相同的运行配置方式。\u003Cbr\u003E虽然平时 socks5 server 主要在 FreeBSD 当中运行,但稳妥起见也要在 Windows 台式机运行同样的程序,用作 backup 方案。\u003Cbr\u003E最重要的一点,两个平台都要采用 Native 编译方式,而不是单纯的“妥协”编译方式。直白点说就是:\u003C/p\u003EWindows 可以用 MSVC 直接编译,不需要经过 Cygwin / MinGW 绕一层。FreeBSD 可以用内置编译器 + BSD Make 直接编译(即使是 cmake 生成的),不需要 GNU Make ,不需要额外再安装编译器。\u003Cp\u003E一直想试试 ASIO 库的协程模式,毕竟写起来流畅多了。我个人记忆力不太好,callback 数量一多就头大,实在记不过来。趁着有需求,正好拿来试一试。\u003C/p\u003E小提示\u003Cp\u003E由于这个程序只是个 Demo ,用是能用。只不过暂时不支持侦听到具体的地址,不提供日志记录,连接超时的时间判断是硬编码的。如果要其他额外功能都实现的话,就不能单靠一个源文件了,只能另开新 repo 把各部份拆开来重新梳理一遍。以后再在新 repo 内慢慢拆、慢慢补。\u003C/p\u003E',ipRegion:'',updatedByUid:0,createdAt:'2025-03-09 23:51:59',updatedAt:'2025-03-11 13:07:39',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5845,num:1,uid:1287,content:'的确,socks5 算是最简单实用的 tcp 编程了,要是去掉 ipv6, bind, udp, 还可以更简单。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-10 04:17:23',updatedAt:'2025-03-11 13:07:39',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5846,num:2,uid:2766,content:'Tab 缩进难受',ipRegion:'',updatedByUid:0,createdAt:'2025-03-10 08:41:28',updatedAt:'2025-03-11 13:07:39',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5847,num:3,uid:2767,content:'asio 已经支持协程了吗',ipRegion:'',updatedByUid:0,createdAt:'2025-03-10 10:27:46',updatedAt:'2025-03-11 13:07:39',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5848,num:4,uid:2768,content:'膜拜',ipRegion:'',updatedByUid:0,createdAt:'2025-03-10 11:28:23',updatedAt:'2025-03-11 13:07:39',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5849,num:5,uid:140,content:'我记得 2022 年,ASIO 开始支持协程。刚看了下 Github 和作者自己写的 Examples ,应该是在 2021 年 12 月的 1.22.0 开始支持的:\u003Cbr\u003Ehttps://think-async.com/Asio/asio-1.22.0/doc/asio/examples.html\u003Cbr\u003Ehttps://github.com/chriskohlhoff/asio/tree/asio-1-22-0\u003Cbr\u003E\u003Cbr\u003E我现在才开始用算是有点晚,当然这也是为了等待编译器更新以及作者修 bug\u003Cbr\u003EASIO 最新版(目前是 1.30.2 )用起来还算顺畅',ipRegion:'',updatedByUid:0,createdAt:'2025-03-10 14:34:29',updatedAt:'2025-03-11 13:07:39',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5850,num:6,uid:140,content:'哈哈哈,这是 Eric Allman 风格,原版的 Allman Style 缩进就是 Tab\u003Cbr\u003E\u003Cbr\u003E我长期用 C# ,习惯了 Allman 缩进方式\u003Cbr\u003Ehttps://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions#style-guidelines\u003Cbr\u003E\u003Cbr\u003EC# 用空格缩进,到了 C++我就索性使用原版 Allman 缩进风格',ipRegion:'',updatedByUid:0,createdAt:'2025-03-10 15:05:53',updatedAt:'2025-03-11 13:07:39',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5851,num:7,uid:2769,content:'毕业后就没怎么写过 C++ 了,几年没关注,现在都已经有原生协程了,有点感慨\u003Cbr\u003E\u003Cbr\u003E当然以前也可以用 boost.context 之类的实现,只不过语法看上去并不优雅便是了\u003Cbr\u003E\u003Cbr\u003E---\u003Cbr\u003E\u003Cbr\u003E题外话,记得上学那会儿为了练手,搞了个 boost.asio 的 PHP binding ,用 PHP 的 generator 实现的协程:\u003Cbr\u003Ehttps://web.archive.org/web/20201102142032/https://github.com/Ci**onX/php-asio/wiki/Basic-concepts-of-php-asio#231-generator\u003Cbr\u003E\u003Cbr\u003E(不过由于水平太菜,做出来的东西性能还不如 AMPHP 之类的纯 PHP 实现,还有内存泄漏的问题不知道该怎么解决,早已删库跑路,哈哈)',ipRegion:'',updatedByUid:0,createdAt:'2025-03-10 15:54:20',updatedAt:'2025-03-11 13:07:39',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},],usersMap:new Map([[2766,{uid:2766,url:'/u/360d5a046a045c75676c506f472f1d00',avatar:'/a/360d5a046a045c75676c506f472f1d00',username:'aqtata🤖',}],[2767,{uid:2767,url:'/u/38196a056a045c75676c516f050f1e25',avatar:'/a/38196a056a045c75676c516f050f1e25',username:'nmap🤖',}],[140,{uid:140,url:'/u/2e317f3e6a045c77616e566f45116522',avatar:'/a/2e317f3e6a045c77616e566f45116522',username:'cnbatch🤖',}],[2769,{uid:2769,url:'/u/170606156a045c75676c5f6f331f1f74',avatar:'/a/170606156a045c75676c5f6f331f1f74',username:'WorseIsBetter🤖',}],[2768,{uid:2768,url:'/u/3d2474176a045c75676c5e6f213f353e',avatar:'/a/3d2474176a045c75676c5e6f213f353e',username:'slideclick🤖',}],[1287,{uid:1287,url:'/u/103e5d226a045c766262516f2e253977',avatar:'/a/103e5d226a045c766262516f2e253977',username:'mayli🤖',}],]),related:[{title:'c++调试遇到奇怪问题',url:'/d/2e707d5d6a045c77606a567a4658616a28306b21',},{title:'求推荐 C++ 高性能的 web 框架, curd 用',url:'/d/1431451d6a045c77606a56754f51616a07712204',},{title:'[全职招聘]C++精通 QT 开发',url:'/d/3e1e59396a045c77606a56744f586b6a140c1565',},{title:'重复造轮子了,用 c++写了个类 rustdesk demo 版本',url:'/d/347171556a045c77606a5674425b616a3f09301d',},{title:'C++ 开发不想 996,如何跳槽转到 Java ?',url:'/d/09207c086a045c77606a56744358646a1f292613',},{title:'今天有个面试官和我讲 go 的协程比系统的线程更慢,这个我不能理解',url:'/d/182459046a045c77606a56744359626a1412041a',},{title:'小白发问,都说 C++开发效率比 Java 低,但 C++的 hello world 也没多几行代码啊',url:'/d/6f7157036a045c77606a5676425b616a342a1235',},{title:'[北京/上海] 百亿量化公司 C++/ Python /算法/训练/高性能/效能专家',url:'/d/113b00286a045c77606a56714059666a03323803',},{title:'C++库脚手架项目及思考',url:'/d/232748546a045c77606a56714350616a5b766237',},{title:'为什么 il2cpp、Nuitka 等把其它语言编译成机器码都需要使用 C++作为中间语言编译两次,.NET 8 的 AOT 可以一步到位?中间语言为什么是 C++,不是 C、更安全的 Rust?',url:'/d/6f3844176a045c77606a5671435a646a5f7a1515',},{title:'[开源] AltTaber: 一款 MacOS 风格的 Alt+Tab 窗口/应用切换器',url:'/d/350f5d286a045c77606a56704450606a0d166205',},{title:'有人遇到过 vs2022 c++代码高亮不生效的问题吗?',url:'/d/6f1b74596a045c77606a56734e5e646a5d001935',},{title:'RISCV 因为两个函数不在 2G 范围内,所以报错 relocation trancated to fit?',url:'/d/682c57206a045c77606a56724051646a29013d35',},{title:'现在来看, C/C++ 的 int、long 等不定宽类型是失败的设计吗?',url:'/d/292a0b046a045c77606a5672435a636a28736003',},],} const App={setup(){const goTo=useGoTo() const{mdAndUp}=useDisplay() return{goTo,mdAndUp}},data(){return data;},mounted(){const themeDark=localStorage.getItem("themeDark") if(themeDark!==null){this.theme.dark=JSON.parse(themeDark)} if(this.nav.post.total>(this.nav.post.currentPage-1)*100+20){let moreLen=100 if(this.nav.post.total({id:null,num:(this.nav.post.currentPage-1)*100+v,uid:null,content:null,ipRegion:null,updatedByUid:null,createdAt:null,updatedAt:null,mentionNum:null,mentionedBy:null,mentionUsers:null,likeUsers:null,})) this.posts.push(...morePosts.slice(20))} this.workerStart() const hash=window.location.hash const match=hash.match(/#(\d+)/) if(match){const n=parseInt(match[1],10) if(n>=(this.nav.post.currentPage-1)*100&&n{this.jumpTo(n)})}} this.$nextTick(()=>{this.addHeadingIds() tocbot.init({tocSelector:'.toc',contentSelector:'#post-content-0',headingSelector:'h2, h3, h4',headingsOffset:100,scrollSmoothOffset:-100,scrollSmooth:true,collapseDepth:6,onClick:function(e){setTimeout(()=>{history.replaceState(null,'',window.location.pathname+window.location.search)},0)},}) tocbot.refresh()});},beforeUnmount(){this.workerStop() if(this.quill){this.quill.destroy() this.quill=null}},computed:{dposts(){return this.posts.slice(20);},},created(){},methods:{successAlert(msg){this.alert={show:true,color:'success',text:msg,timeout:1500,}},failureAlert(msg){this.alert={show:true,color:'error',text:msg,timeout:5000,}},flipThemeDark(){this.theme.dark=!this.theme.dark localStorage.setItem("themeDark",JSON.stringify(this.theme.dark))},toSearch(){if(!this.search.text){this.failureAlert('搜索词不能为空') return} let keywords=this.search.text.trim() if(keywords.length<1){this.failureAlert('搜索词不能为空') return} if(keywords.length>100){this.failureAlert('搜索词过长') return} this.doSearch(keywords)},toReg(){window.location.href="/reg"},toLogin(){window.location.href="/login"},toPage(){let url=window.location.href url=url.replace(/(\/\d+)?(#[0-9]+)?$/,this.nav.post.targetPage>1?`/${this.nav.post.targetPage}`:'') window.location.href=url},toLoadRelated({done}){if(this.my&&this.my.uid){this.apiLoadRelated({done})}else{done('ok')}},workerStart(){this.nav.post.worker=setInterval(()=>{this.workerLoad()},500);},workerStop(){if(this.nav.post.worker){clearInterval(this.nav.post.worker);this.nav.post.worker=null;}},async jumpTo(num){const page=Math.floor(num/100)+1 const i=num-(page-1)*100 if(page===this.nav.post.currentPage){this.goTo("#post-"+num,this.nav.post.goToOptions) if(!this.posts[i].id){const block=Math.floor(num/20)+1 this.nav.post.apiLock[block]=true await this.apiLoadPosts(block) this.$nextTick(()=>{this.goTo("#post-"+num,this.nav.post.goToOptions)})}}else{let url=window.location.href url=url.replace(/(\/\d+)?(#[0-9]+)?$/,page>1?`/${page}`:'') url=url+"#"+num window.location.href=url}},postIntersect(num){return(isIntersecting,entries,observer)=>{if(isIntersecting){this.nav.post.task.push(num) this.nav.post.active.push(num) this.nav.post.active=this.nav.post.active.filter(item=>Math.abs(item-num)<=5) this.nav.post.active.sort((a,b)=>a-b)}else{this.nav.post.active=this.nav.post.active.filter(item=>item!==num)} if(this.nav.post.active[0]){this.nav.post.anchor=this.nav.post.active[0]}else{this.nav.post.anchor=0}}},async apiLoadPosts(block){try{const response=await axios.post('/fapi/v1/post/block/'+block,{discussionId:this.nav.post.discussionId,}) if(response.data.code===0){response.data.data.posts.forEach(post=>{const i=post.num%100 Object.assign(this.posts[i],post)}) response.data.data.users.forEach(user=>{this.usersMap.set(user.uid,user)})}else{this.failureAlert('回帖数据加载失败: '+response.data.msg)}}catch(error){this.failureAlert('回帖数据加载失败: '+error)} this.nav.post.apiLock[block]=false},workerLoad(){while(this.nav.post.task.length){const num=this.nav.post.task.pop() const i=num-(this.nav.post.currentPage-1)*100 if(!this.posts[i].id){const block=Math.floor(num/20)+1 if(!this.nav.post.apiLock[block]){this.nav.post.apiLock[block]=true this.apiLoadPosts(block)}}}},getTimeInfo(t){if(!t){return ""} const now=new Date();const then=new Date(t);const diff=now-then;const minute=60*1000;const hour=minute*60;const day=hour*24;const month=day*30;const year=month*12;if(diffpost.num===num) if(!post){return "#"+num} const uid=post.uid const username=this.usersMap.get(uid)?.username if(!username){return "#"+num} return username},getUsernameByPostId(id){const post=this.posts.find(post=>post.id===id) if(!post){return "#"+this.getPostNumByPostId(id)} const uid=post.uid const username=this.usersMap.get(uid).username if(!username){return "#"+this.getPostNumByPostId(id)} return username},getPostNumByPostId(id){const post=this.posts.find(post=>post.id===id) return post.num},getPostById(id){const post=this.posts.find(post=>post.id===id) return post},getPostByNum(num){const post=this.posts.find(post=>post.num===num) return post},getAvatarByUid(uid){const avatar=this.usersMap.get(uid)?.avatar if(!avatar){return this.getRandomAvatar()} return avatar},getAvatarByPostNum(num){const post=this.posts.find(post=>post.num===num) if(!post){return this.getRandomAvatar()} const uid=post.uid return this.getAvatarByUid(uid)},getRandomAvatar(){const num=Math.floor(Math.random()*100) return "https://randomuser.me/api/portraits/men/"+num+".jpg"},getUrlByUid(uid){const url=this.usersMap.get(uid)?.url if(!url){return ""} return url},getTextByPostNum(num){const post=this.posts.find(post=>post.num===num) if(!post||!post.content){return '点击跳转到#'+num+'查看'} const parser=new DOMParser() const doc=parser.parseFromString(post.content,'text/html') const text=doc.body.textContent||'' return text.slice(0,100)},addHeadingIds(){const content=document.getElementById('post-content-0') if(!content){this.nav.showTOC=false return} const headings=content.querySelectorAll('h2, h3, h4') headings.forEach((heading,index)=>{if(!heading.id){heading.id=`toc-nav-${index}`}}) if(headings.length==0){this.nav.showTOC=false}},async doSearch(keywords){this.search.loading=true try{const response=await axios.post('/fapi/v1/search',{keywords:keywords,}) if(response.data.code===0){if(response.data.data.hash&&response.data.data.hash.length===32){window.location.href="/s/"+response.data.data.hash}else{this.failureAlert('搜索失败: 搜索服务异常')}}else{this.failureAlert('搜索失败: '+response.data.msg)}}catch(error){this.failureAlert('搜索失败: '+error)} this.search.loading=false},debounce(fn,delay){let timer=null return function(...args){if(timer)clearTimeout(timer) timer=setTimeout(()=>{fn.apply(this,args)},delay);};},},watch:{'nav.post.targetPage':{handler:async function(newV,oldV){this.toPage()},immediate:false},},} const vuetify=createVuetify({defaults:{global:{ripple:true,},},}) const app=createApp(App) app.use(vuetify).mount("#app")