废旧android手机变短信服务器

前言

之前有过段时间在想淘汰的android机怎么废物利用起来,也搜索过类似的文章做闹钟、做相框、做网络视频监控。我觉得这都不能发挥一个手机的功能所以一直没着手。但最近我有了新的想法把废旧的android机做成短信服务器提供服务(类似于腾讯、阿里等的短信包)用于消息推送提醒。

正文

原理说明

  1. 手机(android5.0及以上)上安装termux、termux-api (提供linux环境、发短信命令)
  2. 实现内网穿透,以达到外网控制手机。说说的内网穿透:作者有台腾讯服务器,使用rabbitmq实现。后来想想可以简单点是不是直接ssh代理就可以

实现过程

开发环境

  • 手机上安装termux 、termux-api(请使用 com.termux.api_0.31.apk版本由于google协议要求之后的版本去除发短信功能)。安装参考Termux 高级终端安装使用配置教程
    安装完成之后可以测试发短信命令
    termux-sms-send -n 10010 helloworld
  • python 环境安装
pkg install python 
pip install pika #rabbitmq python库

到此开发环境基本完成

代码开发

  • 手机端开发完成连接云服务器,监听命令
coding:utf-8
import pika
import os

credentials=pika.PlainCredentials('用户名', '密码')
parameters = pika.ConnectionParameters(host='远端服务地址', 
    port=5672, virtual_host='/', credentials=credentials)
connection = pika.BlockingConnection(parameters)

channel = connection.channel()
channel.queue_declare(queue='hello')

def callback(ch, method, properties, body):
'''消息的回调方法'''
    print(" [x] Received %r" % body)
    cmds = body.decode().split('|') #协议报文
    if len(cmds)>0 and cmds[0]=='sms':
        os.system('termux-sms-send -n %s %s >/dev/null 2>&1' % (cmds[1], cmds[2])) # 发短信
    elif len(cmds)>0 and cmds[0]=='call':
        os.system('termux-telephony-call %s >/dev/null 2>&1' % (cmds[1])) # 打电话
    print(" [x] over")

channel.basic_consume('hello', callback, auto_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
  • 消息发送端开发
# -*- coding: utf-8 -*-
import pika
import random

class smsServer:
'''云端发送消息报文'''
    def __init__(self):
        self.connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
        self.channel = self.connection.channel()
        self.channel.queue_declare(queue='hello')

    def sms(self, tel, body):
        msg='sms|'+str(tel)+'|'+body
        self.channel.basic_publish(exchange='', routing_key='hello', body=msg)
        print(" [x] Sent: %s" % msg)
        self.connection.close()

    def call(self, tel):
        msg='call|'+str(tel)
        self.channel.basic_publish(exchange='', routing_key='hello', body=msg)
        print(" [x] Sent: %s" % msg)
        self.connection.close()

if __name__=='__main__':
    send = smsServer()
#    send.sms('18679033731', '你好')
    send.call('18679033731')  

到此功能都以完成。

其它

拨打电话接口termux-telephony-call只能拨打电话,但我并没有发现挂断电话的方法。比如发送keyevent或模拟触屏等。所以如果想实现电话炸弹那种接通自动挂,可以root手机后使用input命令(已经实现电话砸蛋功能,但过程有点复杂)。

结尾

动手写之前也琢磨了很久,一篇好的文章怎么让读者看的舒服。想了很久依然没有找到答案,所以文章内容可能会让你一头雾水请多多包含。如有你有兴趣或有疑问可以微信公众号联系。

代码和app请公众号回复[短信服务器]

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章