Beispiel #1
0
def profiles(workspace_id):
    '''
    For GET requests, return all profiles.
    For POST requests, add a new profile.
    '''
    if not validate_workspace(workspace_id):
        return 'workspace does not exist', 404

    if request.method == 'GET':
        all_profiles = Profile.query.filter_by(
            workspace_id=workspace_id).order_by(
                Profile.updated_at.desc()).all()
        schema = ProfileSchema(many=True)
        profiles = schema.dump(all_profiles)
        return jsonify(profiles)
    # request is a POST
    else:
        name = request.form.get('Name')
        from_address = request.form.get('From_Address')
        host = request.form.get('SMTP_Host')
        port = request.form.get('SMTP_Port')
        username = request.form.get('Username')
        password = request.form.get('Password')
        tls = request.form.get('TLS')
        ssl = request.form.get('SSL')

        profile = Profile.query.filter_by(name=name).first()
        ssl_bool = convert_to_bool(ssl)
        tls_bool = convert_to_bool(tls)

        if profile is not None:
            return json.dumps({'success': False}), 200, {
                'ContentType': 'application/json'
            }

        elif type(ssl_bool) != bool or type(tls_bool) != bool:
            return 'ssl/tls must be either true or false', 400


        profile = Profile(name=name, from_address=from_address, smtp_host=host, smtp_port=port, \
            username=encrypt(username.encode()), password=encrypt(password.encode()), tls=tls_bool, ssl=ssl_bool, workspace_id=workspace_id)
        db.session.add(profile)
        update_workspace_ts(Workspace.query.filter_by(id=workspace_id).first())
        db.session.commit()

        schema = ProfileSchema()
        profile_data = schema.dump(profile)
        app.logger.info(
            f'Added profile {name} - Added by {current_user.username} - Client IP address {request.remote_addr}'
        )
        return jsonify(profile_data), 201
Beispiel #2
0
def emails(workspace_id):
    '''
    For GET requests, return all emails for the given workspace.
    For POST requests, add a new email to the given workspace.
    '''
    if not validate_workspace(workspace_id):
        return 'workspace does not exist', 404

    # request is a GET
    if request.method == 'GET':
        all_emails = Email.query.filter_by(workspace_id=workspace_id).order_by(Email.updated_at.desc()).all()
        schema = EmailSchema(many=True)
        email_data = schema.dump(all_emails)
        return jsonify(email_data)

    # request is a POST
    elif request.method == 'POST':
        name = request.form.get('Name')
        html = request.form.get('HTML').encode()
        subject = request.form.get('Subject')
        track = request.form.get('Track')

        track_bool = convert_to_bool(track)
        if type(track_bool) != bool:
            return 'Track must be either true or false', 400

        email = Email(name=name, html=html, subject=subject, workspace_id=workspace_id, track=track_bool)
        db.session.add(email)
        update_workspace_ts(Workspace.query.filter_by(id=workspace_id).first())
        db.session.commit()
        
        schema = EmailSchema()
        email_data = schema.dump(email)
        app.logger.info(f'Added email {name} - Added by {current_user.username} - Client IP address {request.remote_addr}')
        return jsonify(email_data), 200
Beispiel #3
0
def email(workspace_id, email_id):
    '''
    For GET requests, return the given email.
    For DELETE requests, delete the given email.
    For PUT requests, update the given email.
    '''
    if not validate_workspace(workspace_id):
        return 'workspace does not exist', 404

    email = Email.query.filter_by(id=email_id,
                                  workspace_id=workspace_id).first()
    if email is None:
        return 'email does not exist', 404

    #request is a GET
    if request.method == 'GET':
        schema = EmailSchema()
        email_data = schema.dump(email)
        return jsonify(email_data)

    # request is a DELETE
    elif request.method == 'DELETE':
        app.logger.info(
            f'Deleted email {email.name} - Deleted by {current_user.username} - Client IP address {request.remote_addr}'
        )
        db.session.delete(email)
        update_workspace_ts(Workspace.query.filter_by(id=workspace_id).first())
        db.session.commit()
        return 'deleted', 204

    # request is a PUT
    elif request.method == 'PUT':
        name = request.form.get('Name')
        subject = request.form.get('Subject')
        html = request.form.get('HTML').encode()
        track = request.form.get('Track')

        same_email = Email.query.filter_by(name=name).first()

        if same_email is not None and str(same_email.id) != email_id:
            return json.dumps({'success': False}), 200, {
                'ContentType': 'application/json'
            }

        track_bool = convert_to_bool(track)
        if type(track_bool) != bool:
            return 'Track must be either true or false', 400

        email.name = name
        email.subject = subject
        email.html = html
        email.track = track_bool
        update_workspace_ts(Workspace.query.filter_by(id=workspace_id).first())
        db.session.commit()
        app.logger.info(
            f'Updated email {name} - Updated by {current_user.username} - Client IP address {request.remote_addr}'
        )
        return json.dumps({'success': True}), 200, {
            'ContentType': 'application/json'
        }
Beispiel #4
0
def campaigns(workspace_id):
    '''
    For GET requests, return all campaigns for the given workspace.
    For POST requests, all a campaign to the given workspace.
    '''

    if not validate_workspace(workspace_id):
        return 'workspace does not exist', 404

    # request is a GET
    if request.method == 'GET':
        all_campaigns = Campaign.query.filter_by(
            workspace_id=workspace_id).order_by(
                Campaign.updated_at.desc()).all()

        # sort the pages associated with the campaign by index
        # for campaign in all_campaigns:
        #     campaign.pages.sort(key=lambda camp: camp.index)

        schema = CampaignSchema(many=True)
        campaign_data = schema.dump(all_campaigns)
        return jsonify(campaign_data)

    # request is a POST
    elif request.method == 'POST':
        name = request.form.get('Name')
        email_id = request.form.get('Email')
        page_ids = request.form.getlist(
            'Pages[]'
        )  # page names is a list of page names # page names is a list of page names
        profile_id = request.form.get('Profile')
        list_id = request.form.get('List')
        domain_id = request.form.get('Domain')
        server_id = request.form.get('Server')
        port = request.form.get('Port')
        ssl = request.form.get('SSL')
        redirect_url = request.form.get('Redirect_URL')
        safety_url = request.form.get('Safety_URL')
        start_time = request.form.get('Start_Time')
        interval = request.form.get('Interval')
        batch_size = request.form.get('Batch_Size')
        payload_url = request.form.get('Payload_URL')
        payload_file = request.form.get('Payload_File')

        if len(request.files) > 0:
            attach = request.files['Attachment']
            attach_name = attach.filename
            attach_bytes = attach.read()
        else:
            attach_name = None
            attach_bytes = None

        if start_time:
            start_time = convert_to_datetime(start_time)
        else:
            start_time = datetime.now()

        ssl_bool = convert_to_bool(ssl)
        if type(ssl_bool) != bool:
            return 'ssl must be either true or false', 400

        pages = []

        for page_id in page_ids:
            page = Page.query.with_entities(Page).filter(
                (Page.id == page_id) & ((Page.workspace_id == workspace_id)
                                        | (Page.workspace_id == 1))).first()
            pages.append(page)

        email = Email.query.with_entities(Email).filter(
            (Email.id == email_id) & ((Email.workspace_id == workspace_id)
                                      | (Email.workspace_id == 1))).first()
        profile = Profile.query.with_entities(
            Profile).filter((Profile.id == profile_id)
                            & ((Profile.workspace_id == workspace_id)
                               | (Profile.workspace_id == 1))).first()
        targetlist = List.query.with_entities(List).filter(
            (List.id == list_id) & ((List.workspace_id == workspace_id)
                                    | (List.workspace_id == 1))).first()
        domain = Domain.query.filter_by(id=domain_id).first()
        server = Server.query.filter_by(id=server_id).first()

        # make sure all given modules exist before continuing
        makeup = validate_campaign_makeup(email, pages, profile, targetlist,
                                          domain, server)
        if makeup:
            return makeup

        campaign = Campaign(name=name, workspace_id=workspace_id, email_id=email.id, profile_id=profile.id, \
                start_time=start_time, send_interval=interval, batch_size=batch_size, \
                list_id=targetlist.id, domain_id=domain.id, server_id=server.id, port=port, ssl=ssl_bool, redirect_url=redirect_url, \
                safety_url=safety_url, payload_url=payload_url, payload_file=payload_file, attachment=attach_bytes, attachment_name=attach_name)

        db.session.add(campaign)
        update_workspace_ts(Workspace.query.filter_by(id=workspace_id).first())
        db.session.commit()

        for idx, page in enumerate(pages):
            #page_association = Campaignpages(index=idx)
            #campaign.pages.append(page_association)
            page_association = Campaignpages(campaign_id=campaign.id,
                                             page_id=page.id,
                                             index=idx)
            db.session.add(page_association)
            db.session.commit()

        #schema = WorkerCampaignSchema()
        #campaign_data = schema.dump(campaign)
        app.logger.info(
            f'Added campaign {name} (ID: {campaign.id}) (Start time: {start_time}) - Added by {current_user.username} - Client IP address {request.remote_addr}'
        )

        prep_tracking(campaign.list.targets, campaign.id)
        #campaign.cast(campaign_data)
        campaign.cast()

        schema = CampaignSchema()
        data = schema.dump(campaign)

        return json.dumps({
            'success': True,
            'campaign': data
        }), 200, {
            'ContentType': 'application/json'
        }
Beispiel #5
0
def profile(workspace_id, profile_id):
    '''
    For GET requests, return the profile with the given name.
    For POST requests, use the given profile to send a test email.
    For DELETE requests, delete the given profile.
    '''
    if not validate_workspace(workspace_id):
        return 'workspace does not exist', 404

    profile = Profile.query.filter_by(id=profile_id,
                                      workspace_id=workspace_id).first()
    if profile is None:
        return 'profile does not exist', 404

    # request is a GET
    if request.method == 'GET':
        schema = ProfileSchema(strict=True)
        profile_data = schema.dump(profile)
        return jsonify(profile_data)

    # request is a DELETE
    elif request.method == 'DELETE':
        db.session.delete(profile)
        update_workspace_ts(Workspace.query.filter_by(id=workspace_id).first())
        db.session.commit()
        return 'profile deleted', 204

    # request is a POST
    elif request.method == 'POST':
        address = request.form.get('Address')

        if not validate_email_format(address):
            return 'Enter a valid email address', 400

        success = profile.send_test_mail(address)
        return json.dumps({'success': success}), 200, {
            'ContentType': 'application/json'
        }

    # request is a PUT
    elif request.method == 'PUT':
        name = request.form.get('Name')
        from_address = request.form.get('From_Address')
        host = request.form.get('SMTP_Host')
        port = request.form.get('SMTP_Port')
        username = request.form.get('Username')
        password = request.form.get('Password')
        tls = request.form.get('TLS')
        ssl = request.form.get('SSL')

        same_profile = Profile.query.filter_by(name=name).first()

        if same_profile is not None and str(same_profile.id) != profile_id:
            return json.dumps({'success': False}), 200, {
                'ContentType': 'application/json'
            }

        ssl_bool = convert_to_bool(ssl)
        tls_bool = convert_to_bool(tls)

        if type(ssl_bool) != bool or type(tls_bool) != bool:
            return 'ssl/tls must be either true or false', 400

        profile.name = name
        profile.from_address = from_address
        profile.smtp_host = host
        profile.smtp_port = port
        profile.username = username
        profile.password = password
        profile.tls = tls_bool
        profile.ssl = ssl_bool
        profile.workspace_id = workspace_id
        update_workspace_ts(Workspace.query.filter_by(id=workspace_id).first())
        db.session.commit()

        schema = ProfileSchema(strict=True)
        profile_data = schema.dump(profile)
        return jsonify(profile_data[0]), 200
Beispiel #6
0
def campaign(workspace_id, campaign_id):
    '''
    For GET requests, return the given campaign.
    For DELETE requests, delete the given campaign.
    For PUT requests, update the given campaign.
    '''

    if not validate_workspace(workspace_id):
        return 'workspace does not exist', 404

    campaign = Campaign.query.filter_by(id=campaign_id,
                                        workspace_id=workspace_id).first()
    if campaign is None:
        return 'campaign does not exist', 404

    # request is a GET
    if request.method == 'GET':
        schema = CampaignSchema(strict=True)
        campaign_data = schema.dump(campaign)
        return jsonify(campaign_data)

    # request is a DELETE
    elif request.method == 'DELETE':
        if campaign.status == 'Active':
            kill(workspace_id, campaign_id)
        db.session.delete(campaign)
        update_workspace_ts(Workspace.query.filter_by(id=workspace_id).first())
        db.session.commit()
        return 'campaign deleted', 204

    # request is a PUT
    elif request.method == 'PUT':
        name = request.form.get('Name')
        email_name = request.form.get('Email_Name')
        profile_name = request.form.get('Profile_Name')
        list_name = request.form.get('List_Name')
        domain_name = request.form.get('Domain_Name')
        server_alias = request.form.get('Server_Alias')
        port = request.form.get('Port')
        ssl = request.form.get('SSL')
        redirect_url = request.form.get('Redirect_URL')

        same_campaign = Campaign.query.filter_by(name=name).first()

        if same_campaign is not None and str(same_campaign.id) != campaign_id:
            return json.dumps({'success': False}), 200, {
                'ContentType': 'application/json'
            }

        ssl_bool = convert_to_bool(ssl)
        if type(ssl_bool) != bool:
            return 'ssl must be either true or false', 400

        email = Email.query.filter_by(name=email_name,
                                      workspace_id=workspace_id).first()
        profile = Profile.query.filter_by(name=profile_name,
                                          workspace_id=workspace_id).first()
        targetlist = List.query.filter_by(name=list_name,
                                          workspace_id=workspace_id).first()
        domain = Domain.query.filter_by(domain=domain_name).first()
        server = Server.query.filter_by(alias=server_alias).first()

        # make sure all given modules exist before continuing
        makeup = validate_campaign_makeup(email, page, profile, targetlist,
                                          domain, server)
        if makeup:
            return makeup

        campaign.name = name
        campaign.email_id = email.id
        campaign.profile_id = profile.id
        campaign.list_id = targetlist.id
        campaign.domain_id = domain.id
        campaign.server_id = server.id
        campaign.port = port
        campaign.ssl = ssl_bool
        campaign.redirect_url = redirect_url
        update_workspace_ts(Workspace.query.filter_by(id=workspace_id).first())
        db.session.commit()
        return 'campaign updated'
Beispiel #7
0
def campaigns(workspace_id):
    '''
    For GET requests, return all campaigns for the given workspace.
    For POST requests, all a campaign to the given workspace.
    '''

    if not validate_workspace(workspace_id):
        return 'workspace does not exist', 404

    # request is a GET
    if request.method == 'GET':
        all_campaigns = Campaign.query.filter_by(
            workspace_id=workspace_id).order_by(
                Campaign.updated_at.desc()).all()

        # sort the pages associated with the campaign by index
        for campaign in all_campaigns:
            campaign.pages.sort(key=lambda camp: camp.index)

        schema = CampaignSchema(many=True, strict=True)
        campaign_data = schema.dump(all_campaigns)
        return jsonify(campaign_data[0])

    # request is a POST
    elif request.method == 'POST':
        name = request.form.get('Name')
        email_name = request.form.get('Email_Name')
        page_names = request.form.getlist(
            'Page_Names[]'
        )  # page names is a list of page names # page names is a list of page names
        profile_name = request.form.get('Profile_Name')
        list_name = request.form.get('List_Name')
        domain_name = request.form.get('Domain_Name')
        server_alias = request.form.get('Server_Alias')
        port = request.form.get('Port')
        ssl = request.form.get('SSL')
        redirect_url = request.form.get('Redirect_URL')

        ssl_bool = convert_to_bool(ssl)
        if type(ssl_bool) != bool:
            return 'ssl must be either true or false', 400

        pages = []

        for page_name in page_names:
            page = Page.query.with_entities(
                Page).filter((Page.name == page_name)
                             & ((Page.workspace_id == 2)
                                | (Page.workspace_id == 1))).first()
            pages.append(page)

        email = Email.query.with_entities(Email).filter(
            (Email.name == email_name)
            & ((Email.workspace_id == 2) | (Email.workspace_id == 1))).first()
        profile = Profile.query.with_entities(
            Profile).filter((Profile.name == profile_name)
                            & ((Profile.workspace_id == 2)
                               | (Profile.workspace_id == 1))).first()
        targetlist = List.query.with_entities(List).filter(
            (List.name == list_name)
            & ((List.workspace_id == 2) | (List.workspace_id == 1))).first()
        domain = Domain.query.filter_by(domain=domain_name).first()
        server = Server.query.filter_by(alias=server_alias).first()

        # make sure all given modules exist before continuing
        makeup = validate_campaign_makeup(email, pages, profile, targetlist,
                                          domain, server)
        if makeup:
            return makeup

        campaign = Campaign(name=name, workspace_id=workspace_id, email_id=email.id, profile_id=profile.id, \
            list_id=targetlist.id, domain_id=domain.id, server_id=server.id, port=port, ssl=ssl_bool, redirect_url=redirect_url)

        #for page in pages:
        #print(campaign.id)

        db.session.add(campaign)
        update_workspace_ts(Workspace.query.filter_by(id=workspace_id).first())
        db.session.commit()

        for idx, page in enumerate(pages):
            page_association = Campaignpages(campaign_id=campaign.id,
                                             page_id=page.id,
                                             index=idx)
            db.session.add(page_association)
            db.session.commit()

        return json.dumps({
            'success': True,
            'id': campaign.id
        }), 200, {
            'ContentType': 'application/json'
        }