<p id="l55vn"></p>
<nobr id="l55vn"></nobr>
<menuitem id="l55vn"><delect id="l55vn"><i id="l55vn"></i></delect></menuitem>

<p id="l55vn"></p>
      <nobr id="l55vn"><thead id="l55vn"><i id="l55vn"></i></thead></nobr>
      <b id="l55vn"></b>

            <menuitem id="l55vn"></menuitem>
              <nobr id="l55vn"><ruby id="l55vn"><i id="l55vn"></i></ruby></nobr>

              首頁 > 系統 > Linux > 正文

              Linux下完美驅動HTC Android手機的Internet傳輸(通過USB共享電腦網絡)

              2021-12-24 13:38:11
              字體:
              來源:轉載
              供稿:網友

              Htc android手機的Internet傳輸(Internet pass-through)是手機使用USB連接線通過PC上網,共享電腦網絡。

              但是,這么好的功能需要在windows系統下安裝最新版的HTC Sync才能啟動。


              linux下怎么才能使用Internet傳輸功能呢?

              ------------------------------------------------------------

              前期做的研究:Android手機通過USB數據線共享Linux電腦網絡(internet傳輸)

              能勉強使用,但是很麻煩,不完善,缺陷有:

              1.需要手機有root權限。

              2.需要在手機終端上輸入大量命令。

              3.實際聯上網,手機卻一直顯示脫機狀態。

              ------------------------------------------------------------

              Internet傳輸這么好的功能,難道在Linux下就只能這么將就的用著?對Linuxer來說,沒有什么是不可能的!不需root,不需敲命令,手機完美進入internet傳輸狀態,這都可以實現。

              項目地址:https://github.com/qileilu/htcnetup


              一、win下htc sync激活internet傳輸過程——抓包

              要想像htc sync那樣完美的激活internet傳輸,我們先小小的hacker一下這個htc sync。

              這里我使用tcpdump來抓包。

              1.首先把tcpdump文件放到手機的/data/local/tmp/目錄下。(tcpdump下載)

              1 2 adb push tcpdump? /data/local/tmp/ adb shell? chmod? 777? /data/locla/tmp/tcpdump

              2.win下打開終端,轉到adb.exe目錄,插入手機后,運行命令:

              1 adb shell? su? -c? "./data/local/tmp/tcpdump -nnX -i usb0"

              說明:做這一步是需要手機root權限,畢竟是抓包??;命令運行要快,時機很重要,太早adb連接還沒建立,太晚很多包又抓不上;網絡設備是usb0。

              3.抓包結果如下:

              1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 tcpdump: verbose output suppressed, use - v? or -vv? for? full protocol decode listening on usb0, link- type? EN10MB (Ethernet), capture size 96 bytes 15:28:09.408922 IP 192.168.99.89.53370 > 192.168.99.214.6000: S 4103028695:41030 28695(0) win 8192 ???????? 0x0000:? 4500 0034 000f 4000 8006 b234 c0a8 6359? E..4..@...?括cY ???????? 0x0010:? c0a8 63d6 d07a 1770 f48f 3fd7 0000 0000? 括c中z.p???... ???????? 0x0020:? 8002 2000 ea3d 0000 0204 05b4 0103 0308? ....?.....?... ???????? 0x0030:? 0101 0402??????????????????????????????? .... 15:28:09.409135 IP 192.168.99.214.6000 > 192.168.99.89.53370: S 793036861:793036 861(0) ack 4103028696 win 65535 ???????? 0x0000:? 4500 0034 0000 4000 4006 f243 c0a8 63d6? E..4..@.@.駽括c? ???????? 0x0010:? c0a8 6359 1770 d07a 2f44 c83d f48f 3fd8? 括cY.p衵 /D ???? ???????? 0x0020:? 8012 ffff 48a7 0000 0204 05b4 0101 0402? ..H?....?... ???????? 0x0030:? 0103 0301??????????????????????????????? .... 15:28:09.409929 IP 192.168.99.89.53370 > 192.168.99.214.6000: . ack 1 win 256 ???????? 0x0000:? 4500 0028 0010 4000 8006 b23f c0a8 6359? E..(..@...?括cY ???????? 0x0010:? c0a8 63d6 d07a 1770 f48f 3fd8 2f44 c83e? 括c中z.p???D? ???????? 0x0020:? 5010 0100 527e 0000????????????????????? P...R~.. 15:28:09.410600 IP 192.168.99.89.53370 > 192.168.99.214.6000: P 1:5(4) ack 1 win ? 256 ???????? 0x0000:? 4500 002c 0011 4000 8006 b23a c0a8 6359? E..,..@...?括cY ???????? 0x0010:? c0a8 63d6 d07a 1770 f48f 3fd8 2f44 c83e? 括c中z.p???D? ???????? 0x0020:? 5018 0100 5270 0000 0002 0000??????????? P...Rp...... 15:28:09.410753 IP 192.168.99.214.6000 > 192.168.99.89.53370: . ack 5 win 32766 ???????? 0x0000:? 4500 0028 ef33 4000 4006 031c c0a8 63d6? E..(?@.@...括c? ???????? 0x0010:? c0a8 6359 1770 d07a 2f44 c83e f48f 3fdc? 括cY.p衵 /D ???? ???????? 0x0020:? 5010 7ffe 489b 0000????????????????????? P..﨟... 15:28:09.419328 IP 192.168.99.214.6000 > 192.168.99.89.53370: P 1:5(4) ack 5 win ? 32766 ???????? 0x0000:? 4500 002c ef34 4000 4006 0317 c0a8 63d6? E..,?@.@...括c? ???????? 0x0010:? c0a8 6359 1770 d07a 2f44 c83e f48f 3fdc? 括cY.p衵 /D ???? ???????? 0x0020:? 5018 7ffe 536d 0000 8002 0000??????????? P..m...... 15:28:09.636522 IP 192.168.99.89.53370 > 192.168.99.214.6000: . ack 5 win 256 ???????? 0x0000:? 4500 0028 0013 4000 8006 b23c c0a8 6359? E..(..@...?括cY ???????? 0x0010:? c0a8 63d6 d07a 1770 f48f 3fdc 2f44 c842? 括c中z.p???D菳 ???????? 0x0020:? 5010 0100 5276 0000????????????????????? P...Rv.. 15:28:12.636705 IP 192.168.99.89.53370 > 192.168.99.214.6000: P 5:9(4) ack 5 win ? 256 ???????? 0x0000:? 4500 002c 0025 4000 8006 b226 c0a8 6359? E..,.%@...?括cY ???????? 0x0010:? c0a8 63d6 d07a 1770 f48f 3fdc 2f44 c842? 括c中z.p???D菳 ???????? 0x0020:? 5018 0100 5267 0000 0003 0000??????????? P...Rg...... 15:28:12.638841 IP 192.168.99.214.6000 > 192.168.99.89.53370: P 5:9(4) ack 9 win ? 32764 ???????? 0x0000:? 4500 002c ef35 4000 4006 0316 c0a8 63d6? E..,?@.@...括c? ???????? 0x0010:? c0a8 6359 1770 d07a 2f44 c842 f48f 3fe0? 括cY.p衵 /D 菳??? ???????? 0x0020:? 5018 7ffc 5366 0000 8003 0000??????????? P..黃f...... 15:28:12.639848 IP 192.168.99.89.53370 > 192.168.99.214.6000: P 9:13(4) ack 9 wi n 256 ???????? 0x0000:? 4500 002c 0026 4000 8006 b225 c0a8 6359? E..,.&@...?括cY ???????? 0x0010:? c0a8 63d6 d07a 1770 f48f 3fe0 2f44 c846? 括c中z.p???D菷 ???????? 0x0020:? 5018 0100 525d 0000 0005 0000??????????? P...R]...... 15:28:12.640062 IP 192.168.99.89.53370 > 192.168.99.214.6000: F 13:13(0) ack 9 w in? 256 ???????? 0x0000:? 4500 0028 0027 4000 8006 b228 c0a8 6359? E..(.'@...?括cY ???????? 0x0010:? c0a8 63d6 d07a 1770 f48f 3fe4 2f44 c846? 括c中z.p???D菷 ???????? 0x0020:? 5011 0100 5269 0000????????????????????? P...Ri.. 15:28:12.675279 IP 192.168.99.214.6000 > 192.168.99.89.53370: . ack 14 win 32762 ??? ???????? 0x0000:? 4500 0028 ef36 4000 4006 0319 c0a8 63d6? E..(?@.@...括c? ???????? 0x0010:? c0a8 6359 1770 d07a 2f44 c846 f48f 3fe5? 括cY.p衵 /D 菷??? ???????? 0x0020:? 5010 7ffa 489b 0000????????????????????? P..鶫...


              看不懂?沒關系,我看懂了:

              a.手機在電腦生成的網口地址:192.168.99.89 端口53370;手機網口地址:192.168.99.214 端口 6000;

              b.電腦向手機發送的第一次數據:4字節,內容:0002 0000; 手機向電腦應答:4字節 80002 0000;

              c.電腦向手機發送的第二次數據:4字節,內容:0003 0000; 手機向電腦應答:4字節 80003 0000;

              d.電腦向手機發送的第三次數據:4字節,內容:0005 0000; 手機無應答,internet傳輸模式建立。


              好了,有這些就夠了,接下來我們在linux下實現這個過程。


              二、Linux下激活internet傳輸——編程

              有了上述htc sync激活internet傳輸過程的抓包,我們要做的就是在linux下模仿這個激活過程。

              這里我用c來寫。

              1.程序流程

              1)主程序:

              a.接收輸入的手機IP地址

              b.建立socket

              c.建立connect

              d.調用激活子程序

              e.關閉socket退出


              2)激活子程序:

              a.向手機IP的6000端口,發送0x00020000,

              b.收到手機發來的0x8002000后,向手機IP的6000端口發送0x00030000

              c.收到手機發來的0x8003000后,向手機IP的6000端口發送0x00050000


              2.程序代碼

              1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 /* htcnetup.c ? * LuoXiaoqiu ? * An activater for HTC android phone Internet pass-through. ? */ ??? #include #include #include #include ???? #include #include #include//close() #include//struct sockaddr_in #include//inet_ntoa ??? #define DEST_PORT 6000 ??? ??? /* activater process*/ void? process_info( int? s) { ???????? int? send_num; ???????? int? recv_num;?????? ???????? char? recv_buf[4]; ??? ???????? /*the first handshaking*/ ???????? send_num = send(s, "/x00/x02/x00/x00" ,4,0); ???????? if? (send_num < 0){ ???????????? perror ( "send" ); ???????????????? exit (1); ???????? }? else? { ???????? printf ( "send 0x00020000 sucess!/n" ); ???????? } ??? ???????? recv_num = recv(s,recv_buf, sizeof (recv_buf),0); ???????? if (recv_num < 0){ ???????????????? perror ( "recv" ); ???????????????? exit (1); ???????? }? else? { ???????????????? recv_buf[recv_num]= '/0' ; ???????? if? ( strcmp (recv_buf,? "/x80/x02/x00/x00" ) != 0){ ???????????? perror ( "0x80020000 handshaking failure" ); ???????????????????????? exit (1); ???????? }? else? { ???????????? printf ( "recv 0x80020000 sucess!/n" ); ??? ???????????? /*the second handshaking*/ ???????????? send_num = send(s, "/x00/x03/x00/x00" ,4,0); ???????????????????? if? (send_num < 0){ ???????????????????????????? perror ( "send" ); ???????????????????????????? exit (1); ???????????????????? }? else? { ???????????????????????????? printf ( "send 0x00030000 sucess!/n" ); ???????????????????? } ???????? } ???? } ??? ??? ???????? recv_num = recv(s,recv_buf, sizeof (recv_buf),0); ???? if (recv_num < 0){ ???????????????? perror ( "recv" ); ???????????????? exit (1); ???????? }? else? { ???????????????? recv_buf[recv_num]= '/0' ; ???????? if? ( strcmp (recv_buf,? "/x80/x03/x00/x00" ) != 0){ ???????????? perror ( "0x80030000 handshaking failure" ); ???????????????????????? exit (1); ???????? }? else? { ???????????? printf ( "recv 0x80030000 sucess!/n" ); ??? ???????????? /*the third handshaking*/ ???????????? send_num = send(s, "/x00/x05/x00/x00" ,4,0); ???????????????????? if? (send_num < 0){ ???????????????????????????? perror ( "send" ); ???????????????????????????? exit (1); ???????????????????? }? else? { ???????????????? printf ( "send 0x00050000 sucess!/n" ); ???????????????????? } ???????????? ???????? } ???? } ???? printf ( "the activater process success!/n" ); } ??? ??? ??? int? main( int? argc, char? *argv[]) { ???????? int? sock_fd; ???????? struct? sockaddr_in addr_serv; ??? ???? /* check args */ ???? if (argc != 2) ???? { ???? printf ( "usage: htcnetup /n" ); ???? exit (1); ???? } ??? ??? ???????? sock_fd = socket(AF_INET,SOCK_STREAM,0); ???????? if (sock_fd < 0){ ??????????????? perror ( "sock" ); ???????????????? exit (1); ???????? }? else? { ???????????????? printf ( "sock sucessful/n" ); ???????? } ???????? memset (&addr_serv,0, sizeof (addr_serv)); ???????? addr_serv.sin_family = AF_INET; ???????? addr_serv.sin_port =? htons(DEST_PORT); ???????? addr_serv.sin_addr.s_addr = inet_addr(argv[1]); ??????? if ( connect(sock_fd,( struct? sockaddr *)&addr_serv, sizeof ( struct? sockaddr)) < 0){ ???????????????? perror ( "connect" ); ???????????????? printf ( "connect (%d)/n" , errno ); ???????????????? exit (1); ??????? }? else? { ???????????????? printf ( "connect sucessful/n" ); ??????? } ???????? process_info(sock_fd); ???????? close(sock_fd); }

              3.程序編譯執行

              gcc編譯:

              1 gcc? -o htcnetup htcnetup.c

              執行:

              1 . /htcnetup? 192.168.99.214

              這里的命令參數192.168.99.214是手機的IP地址。手機ip地址可以用下列命令獲?。?br />

              1 grep? -e? "usb0"? /proc/net/arp? |? grep? -oP? "/d+/./d+/./d+/./d+"

              執行完程序后,你會發現手機狀態欄久違的internet傳輸圖標出現了!手機的internet傳輸功能在linux下完美激活!

              看圖左上角:


              三、Linux下的包轉發和網關服務

              手機的internet傳輸激活之后(或者說最好是之前),還需要設置一下Linux系統的包轉發。

              網關服務在 Linux 系統中非常容易實現的,兩條命令即可完成:

              1 2 sudo? sysctl net.ipv4.ip_forward=1 sudo? iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE? # eth0 是 PC 端 Internet 出口網絡接口名稱

              DNS 服務在 Linux 系統中也有很多實現,不過如果是訪問 Internet 的話,還是使用 Iptables 轉到 8.8.8.8 解決更簡便:

              1 sudo? iptables -t nat -A PREROUTING -i usb0 -p udp -m udp --dport 53 -j DNAT --to-destination 8.8.8.8:53

              (此處參考:Linux 系統使用 HTC Internet pass-through)


              四、shell腳本編制

              總結前面步驟,首先需設置linux系統網關服務,然后在運行internet傳輸激活程序前,還需知道手機端的ip地址,把這些內容在一個shell腳本中實現如下:

              1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #!/bin/bash ??? #htcnet.sh #LuoXiaoqiu #An activater for HTC android phone Internet pass-through. ??? #PC's internet interface pc_internet_device= "eth0" #phone's connection? interface in pc phone_usb_device= "usb0" ??? sudo? sysctl -w net.ipv4.ip_forward=1 sudo? iptables -t nat -A POSTROUTING -o $pc_internet_device -j MASQUERADE sudo? iptables -t nat -A PREROUTING -i $phone_usb_device -p udp -m udp --dport 53 -j DNAT --to-destination 8.8.8.8:53 ??? get_ip () { ???? arp -n |? grep? $phone_usb_device |? awk? '{print $1}' } ??? #TODO: This needs a timeout and loop needs cleaning up, but works fine and borrowed from another post. echo? "waiting for IP on computer usb" while? [[ `get_ip` < 192 ]]; do? sleep? 2;? done phoneip=`get_ip` echo? "IP adress is $phoneip " ??? #run activater process . /htcnetup? $phoneip

              最后,手機插入電腦usb,運行htcnet.sh腳本,完美實現htc android手機internet傳輸,共享電腦網絡吧??!

              1 2 3 4 5 6 7 8 9 10 11 $ . /htcnet .sh waiting? for? IP on computer usb IP adress is 192.168.99.241 sock sucessful connect sucessful send 0x00020000 sucess! recv 0x80020000 sucess! send 0x00030000 sucess! recv 0x80030000 sucess! send 0x00050000 sucess! the activater process success!

              五、加入udev規則
              插入手機后還得手動運行一個命令激活,能否更方便呢?可以的,我們加入udev規則即可。
              1.把htcnetup和htcnet.sh文件復制到命令搜索目錄:

              1 2 3 4 sudo? cp? htcnetup? /usr/local/bin sudo? cp? htcnet.sh? /usr/local/bin sudo? chmod? a+x? /usr/local/bin/htcnetup sudo? chmod? a+x? /usr/local/bin/htcnet .sh

              2.建立規則文件49-htcpnet.rules,內容如下:

              1 SUBSYSTEM== "usb" , ATTR{idVendor}== "0bb4" , ATTR{idProduct}== "0fb4" ,ACTION== "add" , RUN+= "/usr/bin/sudo -b /usr/local/bin/htcnet.sh"

              把它放到/etc/udev/rules.d/中去:

              1 sudo? cp? 49-htcpnet.rules? /etc/udev/rules .d/

              這樣插入手機,就會自動激活internet 傳輸了!


              六、從git安裝
              這里簡單的說一下怎么從git安裝。

              1 2 3 4 5 6 git clone https: //github .com /qileilu/htcnetup .git cd? htcnetup/ make sudo? make? install 卸載的話: sudo? make? uninstall

              原文地址:Linux下完美驅動HTC Android手機的Internet傳輸(通過USB共享電腦網絡)

              發表評論 共有條評論
              用戶名: 密碼:
              驗證碼: 匿名發表
              a级情欲片在线观看免费
              <p id="l55vn"></p>
              <nobr id="l55vn"></nobr>
              <menuitem id="l55vn"><delect id="l55vn"><i id="l55vn"></i></delect></menuitem>

              <p id="l55vn"></p>
                  <nobr id="l55vn"><thead id="l55vn"><i id="l55vn"></i></thead></nobr>
                  <b id="l55vn"></b>

                        <menuitem id="l55vn"></menuitem>
                          <nobr id="l55vn"><ruby id="l55vn"><i id="l55vn"></i></ruby></nobr>