免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5857 | 回复: 5
打印 上一主题 下一主题

关于用 C 写程序自动登录服务器的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-09-19 16:37 |只看该作者 |倒序浏览
常看到有人问如何用 C 程序自动登录论坛, 邮件服务器, xxx服务器之类的问题.

前不久新浪的 mail 用 foxmail 经常发不出去, 而通过 web 方式则没问题.
新浪邮箱 web 方式登录, 编写, 退出太麻烦, 于是就做了个简单的 WEB 邮件登录/发送代理.
供后来者参考.

工作原理:
(1) smail  连接 sina 登录服务器, 进行登录
(2) 取出登录 id, 请求一个 php 会话服务器(就是它的写邮件的 web站点).
(3) 该站点会返回一个 PHP ID, 和重定向的地址.
(4) 把邮件构造成 multipart/form-data 格式,
(5) 将 登录ID, PHP ID 一起发送给WEB邮件服务器.
(6) 把新浪邮件服务器应答取出, 显示给作用者.
OK 啦.




  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <!--
  4. /* Copyright(C) eyBuild Group, 2005-2007. All Rights Reserved. */
  5. 源码来自: [url]http://www.eybuild.com[/url]
  6. 支持平台: Unix/Windows
  7. Unix编译: make clean all
  8. 变更请通知作者: [email]eybuild@hotmail.com[/email]
  9. -->
  10. <html>
  11. <head>
  12. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  13. <title>新浪邮件</title>
  14. <script language="javascript" src="/js/eyForm.js"></script>
  15. </head>
  16. <style><!--
  17. body,td,a,p,{font-family:arial,sans-serif;font-size:14px}
  18. --></style>
  19. <script language="javascript"><!--
  20. function update_text() {
  21.     form1.msgtxt.value = editbox.document.EditCtrl.document.body.innerHTML;
  22. }
  23. function update_state() {
  24.     msg.innerHTML = "邮件发送中...";
  25. }
  26. --></script>
  27. <body>
  28. <p>
  29. <!--
  30. -->
  31. <center><img src="/img/msina.jpg"></center>
  32. <form name="form1" method="post" action="" ENCTYPE="multipart/form-data" onsubmit="update_state()">
  33. <input type="hidden" name="type" value="html" />
  34. <input type="hidden" name="issend" value="1" />
  35. <input type="hidden" name="fid" value="" />
  36. <input type="hidden" name="mid" value="" />
  37. <input type="hidden" name="youxj" value="3" />
  38. <table width="615" border="0" align="center" cellspacing="0">
  39.     <tr>
  40.         <td colspan="2"><strong>发送新浪邮件</strong></td>
  41.     </tr>
  42.     <tr>
  43.         <td width=100>发送到:</td>
  44.         <td><input name="to" type="text" size="75" value="<% =#G("to") %>" notnull="true"></td>
  45.     <tr>
  46.         <td width=100>抄送到:</td>
  47.         <td><input name="cc" type="text" size="75" value="<% =#G("cc") %>"></td>
  48.     <tr><td width=100>邮件主题:</td>
  49.         <td><input name="subj" type="text" size="75" value="<% /* =G("subj") */ %>" notnull="true"></td>
  50.     <tr>
  51.         <td width=100>发送附件:</td>
  52.         <td><input name="atth0" type="file" size="63" value="<% =G("atth0") %>"></td>
  53.     <tr>
  54.         <td width=100>发送者邮箱:</td>
  55.         <td><input type="text" name="from" size="20" value="<% =G("from") %>" notnull="true">
  56.             登录密码:<input type="password" name="pswd" size="20" value="<% =G("pswd") %>" notnull="true"></td>
  57.     <tr>
  58.         <td colspan=2><textarea id="msgtxt" name="msgtxt" value="" rows=10 cols=65 style="display:none"><% =G("msgtxt") %></textarea>
  59.         <iframe name="editbox" src="edit.htm" height=280 width=100% frameBorder=0 style="border:0px;padding-right: 0pt; padding-left: 0pt; padding-bottom: 0pt; margin: 0pt; "></iframe></td>
  60.     <tr>
  61.         <td><input type="submit" name="sendmail" value="发送邮件" onclick="update_text()"></td>
  62.         <td><input type="button" value="重写邮件"
  63.             onclick="location.href='?from=<% =urlEncode(G("from")) %>&to=<% =urlEncode(G("to")) %>&pswd=<% =G("pswd") %>'" />
  64.             <% CHECK_BOX("nokeep", "1", G("nokeep"), "") %> 不保存已发送邮件.</td>
  65.     </tr>
  66.     <tr><td colspan=2> </td></tr>
  67.     <tr>
  68.         <td colspan=2><font color=red><div id="msg">
  69.             <% if (!isblankstr(errmsg))    /* report error */ {
  70.                    char * success = "\x91\xe9\x80\x81";
  71.                    print("<b>结果:</b> %s", strstr(errmsg, success) ? "发送成功": errmsg);
  72.             } %></div></font></td>
  73.     </tr>
  74. </table>
  75. </form>
  76. <div id="mail_content" style="display: none;">
  77.     <% = strissp(G("nokeep")) ? G("msgtxt"): "" %>
  78. </div>
  79. <xmp><% /* =buff */ %></xmp>
  80. <% @e
  81. #if !defined(REDEF_ENTRY)
  82.     sock_clean();
  83. #endif /* WIN32 */
  84. %>
  85. </body>
  86. </html>

  87. <% @b
  88. @include <socklib.h>
  89.     char                buff[65536] = "";   /* large enougth */
  90.     int                 maxlen = sizeof(buff);
  91.     char                errmsg[256] = "";
  92.    
  93.     if (!strcmp(getRequestMethod(), "POST")) {
  94.         login_send_mail(errmsg, buff, maxlen);
  95.     }
  96. %>

  97. <% @g
  98. /* Copyright(C) eyBuild Group, 2005-2007. All Rights Reserved. */

  99. static
  100. THIS_POST * recv_post(char * errmsg)
  101. {
  102.     int                 max_post_size = 15 * (1024*1024); /* nM */
  103.     POST_PARAM *        p_param = NULL;

  104.     /* has no enough memory */
  105.     if (NULL == (p_param=createPostParam(max_post_size, max_post_size/3, 3, NULL, 0)))
  106.         return NULL;
  107.    
  108.     /* set max size for mail content */
  109.     if (OK != addPostItem(p_param, "msgtxt", 64000)) /* keep 1536 bytes */
  110.         return NULL;
  111.    
  112.     /* done post */
  113.     return dopost(p_param, errmsg);
  114. }


  115. static
  116. int get_login_cookie(char * pbuf, char * cookie)
  117. {
  118.     char *          qstart;
  119.     int             qlen = 0;
  120.    
  121.     /* separate SID */
  122.     if (NULL == (qstart=strstr(pbuf, "SID=")))
  123.         return ERROR;
  124.     qlen = strstr(qstart, ";") - qstart;
  125.     cookie += sprintf(cookie, "%.*s;", qlen, qstart);

  126.     /* separate SPRIAL */
  127.     if (NULL == (qstart=strstr(pbuf, "SPRIAL=")))
  128.         return ERROR;
  129.     qlen = strstr(qstart, ";") - qstart;
  130.     cookie += sprintf(cookie, "%.*s;", qlen, qstart);

  131.     /* separate SINA_USER */
  132.     if (NULL == (qstart=strstr(pbuf, "SINA_USER=")))
  133.         return ERROR;
  134.     qlen = strstr(qstart, ";") - qstart;
  135.     cookie += sprintf(cookie, "%.*s;", qlen, qstart);

  136.     return OK;
  137. }
  138.    
  139. static
  140. int get_phpurl(char * pbuf, char * phpurl)
  141. {
  142.     char *          qstart;
  143.     int             qlen = 0;
  144.    
  145.     /* separate Location: */
  146.     if (NULL == (qstart=strstr(pbuf, "Location: http://")))
  147.         return ERROR;
  148.    
  149.     qstart += 17;
  150.     qlen = strstr(qstart, "/") - qstart;
  151.     strncpy(phpurl, qstart, qlen);
  152.    
  153.     return OK;
  154. }

  155. static
  156. int get_phpsid(char * pbuf, char * cookie)
  157. {
  158.     char *          qstart;
  159.     int             qlen = 0;
  160.    
  161.     /* separate PHPSESSID */
  162.     if (NULL == (qstart=strstr(pbuf, "PHPSESSID=")))
  163.         return ERROR;
  164.    
  165.     qlen = strstr(qstart, ";") - qstart;
  166.     sprintf(cookie+strlen(cookie), "%.*s", qlen, qstart);
  167.    
  168.     return OK;
  169. }

  170. static
  171. int mail_login(char * errmsg, char * pbuf, int maxlen, char * cookie, char * phpurl)
  172. {
  173.     int                 sock = 0;
  174.     char                query[512] = "";
  175.     int                 len;

  176.     /* sendmail */
  177.     if (strissp(G("sendmail")))
  178.         return OK;
  179.       
  180.     if ((sock = sock_connect("mail.sina.com.cn", 80)) < 0)
  181.         return ERROR;
  182.          
  183.     /* make query and http header */
  184.     len = sprintf(query, "site=com&chatlogin=in&l=&user=&nick=&pass=&product=mail&grp=2&"
  185.                          "u=%.16s&psw=%.16s&mynum=1", G("from"), G("pswd"));
  186.     len = sprintf(pbuf, "POST [url]http://mail.sina.com.cn/cgi-bin/login.cgi[/url] HTTP/1.0\r\n"
  187.                      "Content-Type: application/x-www-form-urlencoded\r\n"
  188.                      "Content-Length: %d\r\n"
  189.                      "Host: mail.sina.com.cn\r\n"
  190.                      "\r\n"
  191.                      "%.256s", len, query);

  192.     /* send "login" to server,
  193.      * receive the respond
  194.      */
  195.     if (sock_send(sock, pbuf, len) < 0 || sock_recv(sock, pbuf, maxlen) < 0)
  196.         return ERROR;
  197.     sock_close(sock);
  198.    
  199.     if (OK != get_login_cookie(pbuf, cookie) || OK != get_phpurl(pbuf, phpurl))
  200.     {
  201.         sprintf(errmsg, "Login error.");
  202.         return ERROR;
  203.     }
  204.                
  205.     /*
  206.      * get "PHPSESSID" from mail3-xxx.sinamail.sina.com.cn
  207.      */
  208.     len = sprintf(pbuf, "GET http://%s/cgi/index.php?stype=jingdian HTTP/1.0\r\n"
  209.                      "Host: mail.sina.com.cn\r\n"
  210.                      "Cookie: %s\r\n\r\n",
  211.                      phpurl, cookie);

  212.     /* connect phpserver, to get PHPSESSID cookie */
  213.     if ((sock = sock_connect(phpurl, 80)) < 0)
  214.         return ERROR;
  215.     if (sock_send(sock, pbuf, len)<0 || sock_recv(sock, pbuf, maxlen)<0)
  216.         return ERROR;
  217.     sock_close(sock);

  218.     if (OK != get_phpsid(pbuf, cookie))
  219.     {
  220.         sprintf(errmsg, "Get PHPSESSID error");
  221.         return ERROR;
  222.     }
  223.         
  224.     return OK;
  225. }

  226. static
  227. int parse_send_result(char * errmsg, char * pbuf)
  228. {
  229.     char *          qstart;
  230.     int             qlen = 0;
  231.     char *  str = "<div class=\"sendok_msg\">";
  232.     char *  pstr;
  233.    
  234.     /* separate result */
  235.     if (NULL == (qstart=strstr(pbuf, str)))
  236.     {
  237.         sprintf(errmsg, "%s", "mail server no respond");
  238.         return ERROR;
  239.     }
  240.    
  241.     qstart += strlen(str);
  242.     if (NULL == (pstr=strstr(qstart, "</div>")))
  243.     {
  244.         sprintf(errmsg, "%s", "mail body invalid.");
  245.         return ERROR;
  246.     }
  247.    
  248.     qlen = pstr - qstart;
  249.     strncpy(errmsg, qstart, qlen);
  250.     strtrim(errmsg);
  251.    
  252.     return OK;
  253. }

  254. static
  255. int mail_send(char * errmsg, char * pbuf, int maxlen, const char * cookie, char * phpurl, THIS_POST * p_post)
  256. {
  257.     int                 sock = 0;
  258.     int                 qlen = 0;       /* query header length */
  259.     char                query[8192] = "";
  260.     char *              boundary = "---------------------------7d7138e4400b6";
  261.     char *              list[] = {"type", "issend", "fid", "mid",
  262.                                 "to", "cc", "bcc", "subj", "msgtxt", "youxj"};
  263.     char *              attfile = NULL;
  264.     char                tail[256] = "";
  265.     char *              pstr;
  266.     int                 i, n;
  267.     FILE *              fp;
  268.     char                buf[256];
  269.     int                 len;
  270.         

  271.     if (NULL != (pstr=getContentType()) && NULL != (pstr=strstr(pstr, "boundary=")))
  272.         boundary = pstr + strlen("boundary=");

  273.     n = sizeof(list)/sizeof(list[0]);
  274.     for (len=i=0; i<n; i++)
  275.     {
  276.         len += sprintf(pbuf+len, "--%s\r\nContent-Disposition: form-data; "
  277.                             "name=\"%s\"\r\n\r\n%s\r\n", boundary, list[i], G(list[i]));
  278.     }
  279.    
  280.     if (getPostFileCount(p_post) > 0)
  281.     {
  282.         len += sprintf(pbuf+len, "--%s\r\nContent-Disposition: form-data; "
  283.                             "name=\"atth0\"; filename=\"%s\"\r\n\r\n",
  284.                             boundary, getPostSrcFileName(p_post, 0));
  285.         len += file_size(attfile = getPostTempFileName(p_post, 0));
  286.     }
  287.    
  288.     len += sprintf(tail, "--%s--\r\n\r\n", boundary);
  289.    
  290.     qlen = sprintf(query, "POST http://%s/cgismarty/sendmail.php HTTP/1.0\r\n"
  291.                      "Accept-Language: zh-cn\r\n"
  292.                      "Accept: */*\r\n"
  293.                      "Content-Type: multipart/form-data; boundary=%s\r\n"
  294.                      "Host: %s\r\n"
  295.                      "Content-Length: %d\r\n"
  296.                      "Pragma: no-cache\r\n"
  297.                      "Cookie: %s\r\n\r\n",
  298.             phpurl, boundary, phpurl, len-2, cookie);
  299.             
  300.     /* send ==> receive ==> parse result */
  301.     if ((sock = sock_connect(phpurl, 80)) < 0)
  302.     {
  303.         return ERROR;
  304.     }
  305.    
  306.     /* send post header and post body */
  307.     if (sock_send(sock, query, qlen) < 0 || sock_send(sock, pbuf, 0) < 0)
  308.         return ERROR;

  309.     /* send attached file */
  310.     if (NULL != attfile && NULL != (fp=fopen(attfile, "rb")))
  311.     {
  312.         while(!feof(fp) && (len=fread(buf, 1, sizeof(buf), fp)))
  313.         {
  314.             if (sock_send(sock, buf, len) < 0)
  315.                 return ERROR;
  316.         }
  317.         
  318.         sock_send(sock, "\r\n", 0);
  319.         fclose(fp);
  320.     }
  321.    
  322.     /* send body tail */
  323.     if (sock_send(sock, tail, 0) < 0)
  324.         return ERROR;
  325.    
  326.     if (sock_recv(sock, pbuf, maxlen) < 0)
  327.         return ERROR;
  328.     sock_close(sock);
  329.    
  330.     if (parse_send_result(errmsg, pbuf) < 0)
  331.         return ERROR;

  332.     return OK;
  333. }

  334. int login_send_mail(char * errmsg, char * pbuf, int maxlen)
  335. {
  336.     THIS_POST *         p_post = NULL;
  337.     char                cookie[4096] = "";
  338.     char                phpurl[256] = "";
  339.    
  340.     /* done post */
  341.     if (NULL == (p_post=recv_post(errmsg)) )
  342.     {
  343.         if (strissp(errmsg))
  344.             sprintf(errmsg, "receive post data error");
  345.         return ERROR;
  346.     }
  347.             
  348.     if (mail_login(errmsg, pbuf, maxlen, cookie, phpurl) < 0)
  349.     {
  350.         if (strissp(errmsg))
  351.             sock_error(errmsg);
  352.         return ERROR;
  353.     }
  354.    
  355.     if (OK != mail_send(errmsg, pbuf, maxlen, cookie, phpurl, p_post))
  356.     {
  357.         if (strissp(errmsg))
  358.             sock_error(errmsg);
  359.         return ERROR;
  360.     }
  361.    
  362.     return OK;
  363. }  
  364. %>  

复制代码


附件是编译好的 CGI(windows版),  可在任何支持 cgi 的服务器使用.

[ 本帖最后由 newzy 于 2007-9-19 17:18 编辑 ]

modip.rar

61.13 KB, 下载次数: 134

可用的 cgi(windows版), 可在任何支持 cgi 的服务器使用.

论坛徽章:
0
2 [报告]
发表于 2007-09-19 17:40 |只看该作者
行,我帮您试试!!!

论坛徽章:
0
3 [报告]
发表于 2007-09-19 17:50 |只看该作者

回复 #1 newzy 的帖子

乱码

论坛徽章:
0
4 [报告]
发表于 2007-09-19 20:01 |只看该作者
兄弟用C来实现CGI啊  呵呵  以前我也做过  不现在已经转向了LINUX的C编程,HTTP这块已经放弃了  加油哦

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:53:172015亚冠之水原三星
日期:2015-06-02 16:34:202015年亚冠纪念徽章
日期:2015-10-19 18:13:37程序设计版块每日发帖之星
日期:2015-11-08 06:20:00
5 [报告]
发表于 2007-09-19 22:05 |只看该作者
原帖由 linuxxinetd 于 2007-9-19 20:01 发表
兄弟用C来实现CGI啊  呵呵  以前我也做过  不现在已经转向了LINUX的C编程,HTTP这块已经放弃了  加油哦


然后呢

论坛徽章:
0
6 [报告]
发表于 2007-09-20 17:31 |只看该作者
原帖由 030802127 于 2007-9-19 17:50 发表
乱码

乱码? 哪里, 什么意思?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP